django-views

Django 1.11 TypeError context must be a dict rather than Context

落爺英雄遲暮 提交于 2019-11-27 21:47:50
Just received the Sentry error TypeError context must be a dict rather than Context. on one of my forms. I know it has something to do with Django 1.11, but I am not sure what to change to fix it. Offending line message = get_template('email_forms/direct_donation_form_email.html').render(Context(ctx)) Entire View def donation_application(request): if request.method == 'POST': form = DirectDonationForm(data=request.POST) if form.is_valid(): stripe.api_key = settings.STRIPE_SECRET_KEY name = request.POST.get('name', '') address = request.POST.get('address', '') city = request.POST.get('city', ''

Django models avoid duplicates

耗尽温柔 提交于 2019-11-27 21:02:21
问题 In models: class Getdata(models.Model): title = models.CharField(max_length=255) state = models.CharField(max_length=2, choices=STATE, default="0") name = models.ForeignKey(School) created_by = models.ForeignKey(profile) def __unicode__(self): return self.id() In templates: <form> <input type="submit" value="save the data" /> </form> If the user clicks on the save button and the above data is saved in the table, how to avoid the duplicates, i.e. if the user again clicks on the same submit

Using multiple forms on a page in Django

随声附和 提交于 2019-11-27 20:18:52
How do I use more than one form per page in Django? Herman Schaaf Please see the following previously asked (and answered) questions: Django: multiple models in one template using forms and Proper way to handle multiple forms on one page in Django . Depending on what you're really asking, this is my favorite way to handle different models on the same page: if request.POST(): a_valid = formA.is_valid() b_valid = formB.is_valid() c_valid = formC.is_valid() # we do this since 'and' short circuits and we want to check to whole page for form errors if a_valid and b_valid and c_valid: a = formA.save

Using request.user with Django ModelForm

混江龙づ霸主 提交于 2019-11-27 20:09:39
I'm having a problem with logged users and a Django ModelForm . I have a class named _Animal_ that has a ForeignKey to User and some data related to the animal like age, race, and so on. A user can add Animals to the db and I have to track the author of each animal, so I need to add the request.user that is logged when the user creates an animal instance. models.py class Animal(models.Model): name = models.CharField(max_length=300) age = models.PositiveSmallIntegerField() race = models.ForeignKey(Race) ... publisher = models.ForeignKey(User) def __unicode__(self): return self.name class

Get request.session from a class-based generic view

老子叫甜甜 提交于 2019-11-27 19:40:30
Is there a way to get request.session from inside a class-based view? For instance, I have from django.views.generic.edit import FormView class CreateProfileView(FormView): def form_valid(self, form): # --> would like to save form contents to session here return redirect(self.get_success_url()) The only thing I can think of would be override as_view by adding def as_view(self, request, *args, **kwargs): self.session = request.session super(CreateProfileView, self).as_view(request, *args, **kwargs) to the class. But that seems ugly. Is there another way? Timmy O'Mahony You have access to self

Django print choices value

断了今生、忘了曾经 提交于 2019-11-27 19:36:51
EMP_CHOICES = ( (0,'-- Select --'), (1,'Good'), (2,'Average'), ) class EMPFeedback(models.Model): user_choices = models.IntegerField(choices=EMP_CHOICES) If the value stored in the db as 1 for user_choices how to print the corresponding user_choices corresponding value (i.e. 1==GOOD) fb = EMPFeedback.objects.get(id=1) print fb.user_choices # prints 1 print fb.user_choices.EMP_CHOICES There's a method for that! (™ Apple) fb.get_user_choices_display() 来源: https://stackoverflow.com/questions/4274243/django-print-choices-value

get user profile in django

冷暖自知 提交于 2019-11-27 19:07:10
hello i'm new in python and django I need a view that get current user profile I know I shoud use get_profile from User but I don't know how to use it . i read the django document and It didn't help me. this is what I found from doc: from django.contrib.auth.models import User profile=request.user.get_profile() César Django's documentation says it all, specifically the part Storing additional information about users . First you need to define a model somewhere in your models.py with fields for the additional information of the user: models.py from django.contrib.auth.models import User class

add request.GET variable using django.shortcuts.redirect

时光怂恿深爱的人放手 提交于 2019-11-27 18:18:52
Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) If I do redirect('url-name', x) I get HttpResponseRedirect('/my_long_url/%s/', x) I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering... Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) I don't know of any way to do this without modifying the urls.py . I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering... You might want to write a thin wrapper to

How do I return JSON without using a template in Django?

假如想象 提交于 2019-11-27 16:58:00
This is related to this question: Django return json and html depending on client python I have a command line Python API for a Django app. When I access the app through the API it should return JSON and with a browser it should return HTML. I can use different URLs to access the different versions but how do I render the HTML template and JSON in the views.py with just one template? To render the HTML I would use: return render_to_response('sample/sample.html....') But how would I do the same for JSON without putting a JSON template? (the content-type should be application/json instead of

Forbidden (403) CSRF verification failed. Request aborted

非 Y 不嫁゛ 提交于 2019-11-27 16:33:54
问题 I am making an app of login form but when I am running my app and click on login button the following error will occur Forbidden (403) CSRF verification failed. Request aborted. the code of view.py is as: from django.template import loader from django.shortcuts import render_to_response from registration.models import Registration from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import redirect def view_login(request,registration_id): t =