django-class-based-views

Django - Correct way to pass arguments to CBV decorators?

匆匆过客 提交于 2019-12-22 10:38:52
问题 The docs feature nice options for applying decorators such as login_required to Class Based Views. However, I'm a little unclear about how to pass specific arguments along with the decorator, in this case I'd like to change the login_url of the decorator. Something like the following, only valid: @login_required(login_url="Accounts:account_login") @user_passes_test(profile_check) class AccountSelectView(TemplateView): template_name='select_account_type.html' 回答1: You should use @method

Django IntegrityError at /new null value in column “owner_id” violates not-null constraint

南笙酒味 提交于 2019-12-22 08:14:08
问题 I'm trying to track the user that created an object using a CreateView and I'm doing it exactly like it's done in documentation (https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/, Models and request.user) except I don't use login_required() decorator but LoginRequiredMixin from django-braces instead. My model: class Contact(models.Model): owner = models.ForeignKey(User, editable=False) first_name = models.CharField(max_length=255,) last_name = models.CharField

Why doesn't self.object in a CreateView have an id after saving to the database?

﹥>﹥吖頭↗ 提交于 2019-12-22 05:57:12
问题 Following the comment on the accepted answer on django createview how to get the object that is created, I am attempting to use the id from a user created by a CreateView in its get_success_url method. However, even though it is definitely being saved to MySQL and receiving an id, when I access self.object , it doesn't have an id to use. The model does have an id property. Why wouldn't I be able to access the id? If I'm being led astray by the linked comment, what is the right way to get the

django class-based view - UpdateView - How to access the request user while processing a form?

和自甴很熟 提交于 2019-12-21 09:22:21
问题 In a class-base UpdateView in Django, I exclude the user field as it is internal to the system and I won't ask for it. Now what is the proper Django way of passing the user into the form. (How I do it now, is I pass the user into the init of the form and then override the form's save() method. But I bet that there is a proper way of doing this. Something like a hidden field or things of that nature. # models.py class Entry(models.Model): user = models.ForeignKey( User, related_name="%(class)s

How to do a DetailView in django 1.3?

一个人想着一个人 提交于 2019-12-20 10:55:51
问题 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

Why do I need to decorate login_required decorator with @method_decorator

半城伤御伤魂 提交于 2019-12-20 10:42:05
问题 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

success_message in DeleteView not shown

北战南征 提交于 2019-12-20 09:53:30
问题 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

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

吃可爱长大的小学妹 提交于 2019-12-20 09:38:03
问题 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

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

无人久伴 提交于 2019-12-20 08:56:08
问题 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

Django apps using class-based views and ajax?

吃可爱长大的小学妹 提交于 2019-12-20 08:43:16
问题 I'm learning Django and I found class-based views and I wonder how to implement Ajax on those views. I searched github for a django project and I found some using class-based views but not ajax. So... Anybody knows an open source project that use both things? It easier to learn that way. Thank you :) 回答1: An ajax view isn't much different to a normal view except that you usually want to return a different format then when processing a normal request. This format is usually JSON. The