django-views

Adding extra_context in Django logout built-in view

前提是你 提交于 2019-12-18 05:57:42
问题 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

Adding extra_context in Django logout built-in view

风流意气都作罢 提交于 2019-12-18 05:57:09
问题 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

Determine the requested content type?

五迷三道 提交于 2019-12-18 05:38:01
问题 I'd like to write a Django view which serves out variant content based on what's requested. For example, for "text/xml", serve XML, for "text/json", serve JSON, etc. Is there a way to determine this from a request object? Something like this would be awesome: def process(request): if request.type == "text/xml": pass elif request.type == "text/json": pass else: pass Is there a property on HttpRequest for this? 回答1: HttpRequest.META, more specifically HttpRequest.META.get('HTTP_ACCEPT') — and

Determine the requested content type?

无人久伴 提交于 2019-12-18 05:37:05
问题 I'd like to write a Django view which serves out variant content based on what's requested. For example, for "text/xml", serve XML, for "text/json", serve JSON, etc. Is there a way to determine this from a request object? Something like this would be awesome: def process(request): if request.type == "text/xml": pass elif request.type == "text/json": pass else: pass Is there a property on HttpRequest for this? 回答1: HttpRequest.META, more specifically HttpRequest.META.get('HTTP_ACCEPT') — and

How do I use UpdateView?

爷,独闯天下 提交于 2019-12-18 05:07:10
问题 I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form. Here is my views.py: class RegistrationView(FormView): form_class = RegistrationForm template_name = "register.html" success_url = "/accounts/profile/" def form_valid(self, form): if form.is_valid: user = form.save() user = authenticate(username=user.username, password=form.cleaned_data['password1']) login(self

Filtering a model in a CreateView with get_queryset

徘徊边缘 提交于 2019-12-18 03:43:41
问题 I'm trying to filter a model with get_queryset() and it seems to work in the view but not in the template. My view : class FolderCreate(CreateView): fields = ['name', 'parent'] template_name = 'Form/folder_create.html' def get_queryset(self): folders = Folder.objects.filter(owner=self.request.user) print folders # ==> return [<Folder: Folder>, <Folder: Another folder>] return folders def form_valid(self, form): self.object = form.save(commit=False) self.object.owner = self.request.user return

Getting next and previous objects in Django

大城市里の小女人 提交于 2019-12-18 02:03:30
问题 I'm trying to get the next and previous objects of a comic book issue. Simply changing the id number or filtering through date added is not going to work because I don't add the issues sequentially. This is how my views are setup and it WORKS for prev_issue and does return the previous object, but it returns the last object for next_issue and I do not know why. def issue(request, issue_id): issue = get_object_or_404(Issue, pk=issue_id) title = Title.objects.filter(issue=issue) prev_issue =

Getting next and previous objects in Django

℡╲_俬逩灬. 提交于 2019-12-18 02:03:22
问题 I'm trying to get the next and previous objects of a comic book issue. Simply changing the id number or filtering through date added is not going to work because I don't add the issues sequentially. This is how my views are setup and it WORKS for prev_issue and does return the previous object, but it returns the last object for next_issue and I do not know why. def issue(request, issue_id): issue = get_object_or_404(Issue, pk=issue_id) title = Title.objects.filter(issue=issue) prev_issue =

django-admin: Add extra row with totals

会有一股神秘感。 提交于 2019-12-17 23:20:21
问题 I'm using the standard django admin module to display a list of rows. One of the columns is a numerical field. I'd like to display an extra 'totals' row that has most of the columns as blank, except for the numerical column, which should be the total for all of the objects. Is there a simple way to do this within the admin module, or am I better off making a custom view for it? I'm using Django 1.2. 回答1: Yes, you can do it in many ways, but most django-ist way to do is: First override the

how to get a list of all views in a django application?

谁都会走 提交于 2019-12-17 22:43:37
问题 Is there any way to get a list of all views in an django app? I have googled for answer. All answers shows a way to get list of urls. 回答1: Getting list of all the views of a Django project: To get all the views present in a Django project, we create a function get_all_view_names() which takes urlpatterns as input and returns the complete list of views being used in the project as the output. First, we import the root_urlconf module using settings.ROOT_URLCONF . Then root_urlconf.urls