django-views

Accessing Django OneToOneField in templates?

99封情书 提交于 2019-11-30 17:40:35
I have the following scenario. class A(models.Model): a = models.IntegerField() class B(models.Model): c = models.OneToOneField(A) d = models.DecimalField() In my templates, I have a list of objects A. In my template how do I access the attribute "d" from my template? Thanks. class B(models.Model): c = models.OneToOneField(A, related_name='b') d = models.DecimalField() {{ a.b.d }} 来源: https://stackoverflow.com/questions/6676134/accessing-django-onetoonefield-in-templates

How to wrap a Django Form Wizard in a View?

拜拜、爱过 提交于 2019-11-30 17:39:14
问题 This question is highly related to one that was previously asked and answered here: How wrap a FormWizard in a View? Can someone post exact details of how they have successfully wrapped a Django Form Wizard into a view so that the login_required decorator can be used? There are numerous discussions of this topic on the internet but they all seem to be incomplete because they don't actually show how they have defined their Form Wizard class. When I run point my browser to the view, I get the

Passing a python list to django template

北城余情 提交于 2019-11-30 17:18:52
问题 I want to display a list of things on my template. So I have a view to generate that list and pass it to template like this: newlinks = [] try: links=urllib2.urlopen("<<Some HTML file link>>").readlines() except (urllib2.HTTPError): links = '' pass for link in links: newlinks.append(link[0:-1]) return render_to_response('template11.html', {'links',newlinks}, context_instance=RequestContext(request)) But while rendering it, i get TypeError Exception Type: TypeError Exception Value: unhashable

NameError: name 'request' is not defined, in Django forms

巧了我就是萌 提交于 2019-11-30 16:58:27
How do I get the current logged in user in forms.py ? I am trying to pre-populate the email field of the current user. class ContactMe(forms.Form): name = forms.CharField(label = "Name") email_address = forms.CharField(label = "Email Address", intital = request.user.email) subject = forms.CharField(label = "Subject") message = forms.CharField(label = "Message", widget=forms.Textarea(attrs={'cols': 10, 'rows': 3})) additional_comments = forms.CharField(required = False) class Meta: model = Contact_me I tried passing request from views.py as : contact_form = ContactMe(request.POST or None,

get class name for empty queryset in django

吃可爱长大的小学妹 提交于 2019-11-30 16:52:09
I have empty queryset of model Student students = Students.objects.all() If the above queryset is empty, then how can i get the model(class name)? How can i get the model name for empty queryset? EDIT: How can i get the app name from the queryset? >>> students = Students.objects.all() # The queryset's model class: >>> students.model project.app.models.Student # Name of the model class: >>> students.model.__name__ 'Student' # Import path of the models module: >>> students.model.__module__ 'project.app.models' # Django app name: >>> students.model._meta.app_label 'app' students.model Querysets

Django redirect using reverse() to a URL that relies on query strings

☆樱花仙子☆ 提交于 2019-11-30 16:44:22
问题 I'm writing a django application with a URL like 'http://localhost/entity/id/?overlay=other_id'. Where id is the primary key of the particular entity and overlay is an optional query parameter for a second entity to be overlaid in the display. The user can only ever update an entity when viewing objects through an overlay. When POSTing to /update/id, I want to redirect back to /entity/id, but I don't want to lose my query parameter during the redirect, as the change in view would be jarring.

How can I have Django user registration single step (instead of two step)process with email compulsory?

我的梦境 提交于 2019-11-30 16:21:49
I want Django to send an email to user email-address with Login details once admin adds a new user to admin site.So I tried using Django signals for that but just becoz django user registration is a two step process signals get notified in first step only and called email function without email address(which comes in second step). My signal code: def email_new_user(sender, **kwargs): if kwargs["created"]: # only for new users new_user = kwargs["instance"] send_mail('Subject here', 'Here is the message.', 'from@example.com',['to@example.com'], fail_silently=False) post_save.connect(email_new

How can I have Django user registration single step (instead of two step)process with email compulsory?

混江龙づ霸主 提交于 2019-11-30 16:08:29
问题 I want Django to send an email to user email-address with Login details once admin adds a new user to admin site.So I tried using Django signals for that but just becoz django user registration is a two step process signals get notified in first step only and called email function without email address(which comes in second step). My signal code: def email_new_user(sender, **kwargs): if kwargs["created"]: # only for new users new_user = kwargs["instance"] send_mail('Subject here', 'Here is

How do I delete a session key in Django after it is used once?

只愿长相守 提交于 2019-11-30 14:35:30
问题 I have two views. view1 passes an error message to view2 through a session key. How do I delete the key after view2 is rendered? I only need it for once: redirect from view1 to view2. I dont need that message to show up after refreshing my webpage. I don't think python will continue to execute once it reaches return I was thinking about setting an expiration timestamp but I need to ensure that it exists for at least 10-20 seconds, if the application really does that long to load (we do some

Django Redirect to previous view

不羁的心 提交于 2019-11-30 13:24:40
问题 I have a button on page x and page y that redirects to page z. On page z, I have a form that needs filling out. Upon saving, I want to redirect to page x or y (whichever one I was on initially). Normally, you use "redirect" in the view, and specify the page you want to redirect to. But what would you do in a case like this? Any ideas? Thanks! 回答1: You can use GET parameters to track from which page you arrived to page z. So when you are arriving normally to page z we remember from which page