django-views

Using Django class-based views, how can I return a different template if request.is_ajax

让人想犯罪 __ 提交于 2019-11-29 14:57:32
问题 I find Django's request.is_ajax a very useful way to add progressive enhancement via JS and still keep DRY in my views. However, I want to use class-based views and render with a different template if request.is_ajax. It is not clear to me how I can override my default "template_name" and make the template loading conditional in class-based views. How can I do this? 回答1: The appropriate way to do this is to override the methods provided by the TemplateResponseMixin. If you simply need to

Does Django automatically detect the end user's timezone?

南笙酒味 提交于 2019-11-29 14:29:41
I am building an application in Django which allows the end-user to retrieve information which is sensitive to the time of day (12 am to 12 am) on a given day. I store this information in my database as an integer representing the seconds since midnight in 30-minute increments. I was looking at Django's timezone documentation: https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/ and found myself confused on whether or Django automatically uses the end-users time, or if I must collect this information and account for it in my views. Any information would be helpful! Thanks. No. There

Django 1.5 - using the new StreamingHttpResponse

半城伤御伤魂 提交于 2019-11-29 14:01:25
If I implement StreamingHttpResponse as shown here , the 'streaming' response is not shown until the 10 seconds is up. There isn't much information on djangoproject except saying it's useful for generating large CSV files while warning that expensive tasks should be performed outside of the request-response cycle. However, I cannot see that it is working at all using time-intensive code. Is there something about the generator object that prevents this? Here is the code duplicated for reference. import time from django.http import StreamingHttpResponse def stream_response(request): resp =

Django/python: 'function' object has no attribute 'as_view'

只愿长相守 提交于 2019-11-29 12:43:18
问题 I am trying to create a list_view for a model queryset. When running my server, it returns : attribute error - 'function' object has no attribute 'as_view'. I would appreciate helping me in solve this. Here's my code: Views.py: @login_required class live_bids(ListView): model = Post template_name = 'loggedin_load/live_bids.html' def get_queryset(self): return Post.objects.all().prefetch_related('bids').filter(user=self.request.user) urls.py: url(r'^live_bids/$', live_bids.as_view()), 回答1: You

Dynamically filter ListView CBV in Django 1.7

半腔热情 提交于 2019-11-29 12:37:37
问题 I've read the official documentation on dynamically filtering ListView, but am still confused about how to actually use it. I currently have a simple model, let's call it Scholarship : class Scholarship(models.Model): title = models.CharField(max_length=255) submitted_date = models.DateField(auto_now=True, verbose_name='Date Submitted') EXPERIENCE_LEVEL_CHOICES = ( ('A', 'Any'), ('S', 'Student'), ('G', 'Graduate') ) experience_level = models.CharField(max_length=1, choices=EXPERIENCE_LEVEL

How to extend UserCreationForm with fields from UserProfile

橙三吉。 提交于 2019-11-29 12:31:58
I found this post on how to extend the UserCreationForm with extra fields such as "email." However, the email field is already defined in the pre-built user model. I created an extra model (called UserProfile) that futher extends Django's pre-built User class. How do I get these fields I defined in UserProfile to appear in my UserCreationForm? Add fields as appropriate for your UserProfile model (it's not too easy to use a ModelForm to avoid Repeating Yourself, unfortunately), then create and save a new UserProfile instance in the over-ridden save() function. Adapted from the post you linked

Curious about get_form_kwargs in FormView

元气小坏坏 提交于 2019-11-29 11:19:49
问题 I was having trouble with FormView recently and found that the way to go about doing it was to use get_form_kwargs. Here is my code: class InternalResetPasswordView(FormView): template_name = 'reset_password.html' form_class = forms.InternalPasswordResetForm # success_message = "Password was reset successfully" # To get request object # http://notesondjango.wordpress.com/2012/12/18/modelform-formview-and-the-request-object/ # https://stackoverflow.com/questions/13383381/show-message-after

How do I pass variables from one view to another and render with the last view's URL in Django?

只愿长相守 提交于 2019-11-29 10:42:35
I'm building a student management system using Django. In this code, The user search for a student with the encrypted query name=StudentName&grade=Grade&id=StudentID&phone=ParentPhoneNumber&report=StudentReportNumber , that is extracted with the decrypt() method. Here are the two methods, the one which process the query and the one which shows the student profile. No data from the query is saved to the database, but will be used to query the student details from the database. def process_query(request): # process the query from the url /?details={{ some hashes here }} if request.method == 'GET

Adding extra_context in Django logout built-in view

寵の児 提交于 2019-11-29 10:24:20
In django/contrib/auth/views.py there is the definition of the logout view : def logout(request, next_page=None, template_name='registration/logged_out.html', redirect_field_name=REDIRECT_FIELD_NAME, current_app=None, extra_context=None): I would like to add extra_context to get rid of the 'Logged out' title that appear when I log off so I'm trying this in my url confs : (r'^accounts/logout/$', logout(extra_context={'title':'something else'}) ), but then I get this error : logout() takes at least 1 non-keyword argument (0 given) what I'm doing wrong? ps: when I do (r'^accounts/logout/$',

How can I display a user profile using Django? [closed]

点点圈 提交于 2019-11-29 09:51:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I am new to django and I am currently trying to build a website that allows users to sign in and view other users profiles. So far I have managed to let users sign in but I can't work out how to view other peoples profiles. Each profile uses the users username to create a url for