django-class-based-views

How to unit test methods inside django's class based views?

别等时光非礼了梦想. 提交于 2019-12-03 12:33:01
I need to test the methods and helper function inside a django Class Based View. Consider this Class Based View: class MyClassBasedView(View): def dispatch(self, request, *args, **kwargs): .... def __get_render_dict(): d = {} ... return d def my_method(self): render_dict = self.__get_render_dict() return render_response(self.request, 'template.html', render_dict) In order to write unit tests for my view, I need to call the methods inside, say __get_render_dict() directly. How can I achieve this?. I've tried v = MyClassedBasedView() v.dispatch(request,args, kwargs) v.__method_name() but this

How do you use get_context_data with TemplateView in Django [closed]

二次信任 提交于 2019-12-03 04:44:02
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm trying to do something like this: class AboutView(TemplateView): template_name = 'about.html' def get_context_data(self, **kwargs): context = super(AboutView, self).get_context_data(**kwargs) context['dahl

Why do I need to decorate login_required decorator with @method_decorator

雨燕双飞 提交于 2019-12-03 01:11:44
I am trying to understand the code for the mixins posted at this blog post . These mixins call the login_required decorator from django.contrib.auth.decorators within the mixins , but they do so decorated by the method_decorator from django.utils.decorators . In the sample code below I dont understand why I need to decorate the login_required decorator . from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required class LoginRequiredMixin(object): """ View mixin which verifies that the user has authenticated. NOTE: This should be the left-most

How to do a DetailView in django 1.3?

老子叫甜甜 提交于 2019-12-03 00:40:37
I'm currently learning how to use the class-based views in django 1.3. I'm trying to update an application to use them, but I still don't uderstand very well how they work (and I read the entire class-based views reference like two or three times EVERY day). To the question, I have an space index page that needs some extra context data, the url parameter is a name (no pk, and that can't be changed, it's the expected behaviour) and the users that don't have that space selected in their profiles can't enter it. My function-based code (working fine): def view_space_index(request, space_name):

success_message in DeleteView not shown

梦想的初衷 提交于 2019-12-02 22:34:49
I have a DeleteView: class LectureDelete(SuccessMessageMixin, DeleteView): model = Lecture success_message = "Die Veranstaltung wurde gelöscht" success_url = '/' def get_object(self): qs = super(LectureDelete, self).get_object() if self.request.user.has_perm('edit_lecture', qs): return qs else: raise exceptions.PermissionDenied And in my template to which the success_url links, I have the following code, which works fine with other messages: {% if messages %} {% for message in messages %} <p class="alert alert-dismissable {% if message.tags %}alert-{{ message.tags }}"{% endif %}> <button type=

Django - UpdateView with inline formsets trying to save duplicate records?

≯℡__Kan透↙ 提交于 2019-12-02 19:25:43
I have an Expense model and an ExpenseLineItem model. Just like a typical expense/invoice, one expense can have several line items to make up the total cost of an invoice. I'm trying to use class based views to create and update expenses. I've successfully coded the CreateView to make a new expense with multiple expense line items. My problem is when I try and update an existing Expense which already has several expense line items. Here's my code below, and I can't figure out what the issue is. The mixins ( TitleMixin , CancelSuccessMixin , SelectedApartment )are mine and work fine. I'm

How do you use get_context_data with TemplateView in Django [closed]

删除回忆录丶 提交于 2019-12-02 17:55:09
I'm trying to do something like this: class AboutView(TemplateView): template_name = 'about.html' def get_context_data(self, **kwargs): context = super(AboutView, self).get_context_data(**kwargs) context['dahl_books'] = Books.objects.filter(author="Dahl') When I try to access dahl_books in my template like this: {% for book in dahl_books %} dahl_books is not available in the template context, even though the Books QuerySet returned a non-zero number of books. ....am I doing something wrong in either my template or in get_context_data ? I can't test it, but I bet you need return context at the

Django - Class Based Generic View - “No URL to redirect to”

ε祈祈猫儿з 提交于 2019-12-02 17:24:45
I'm using the generic CreateView like: #urls.py from django.conf.urls.defaults import * from django.views.generic import CreateView from content.models import myModel urlpatterns = patterns('myApp.views', (r'myCreate/$', CreateView.as_view(model=myModel)), ) With a mymodel_form.html template like: <form method="post" action=""> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit" /> </form> When I submit my form, the new object is created but I get the error ImproperlyConfigured at ... No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.

NoReverseMatch django class based

女生的网名这么多〃 提交于 2019-12-02 15:12:05
问题 edit. sorry I'm having a hard time and thanks for your help how do I do that? model created_by = models.ForeignKey(User) def get_absolute_url(self): return reverse('author_update', kwargs={'pk': self.pk, 'user_id': self.created_by}) Could be related to the error Type error with django class based view I get this error NoReverseMatch at /author/add/4 Reverse for 'author_add' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['author/add/(?P<user_id>\\d+)$'] urls.py

Django(trunk) and class based generic views: one form's initial data appearing in another one's

╄→гoц情女王★ 提交于 2019-12-02 11:52:47
I've run into a strange problem where data seems to persist accross different views and requests until the server gets restarted. I've managed to reduce the issue to the following code: # foobar/models.py from django.db import models class Foo(models.Model): bug = models.CharField(max_length=10) # foobar/forms.py from django import forms from foobar.models import Foo class CreateForm(forms.ModelForm): class Meta: model = Foo class UpdateForm(forms.ModelForm): class Meta: model = Foo def __init__(self, *args, **kwargs): kwargs.setdefault('initial', {}) kwargs['initial'].update({'bug': 'WHY??'})