django-class-based-views

Django: authenticate based on an object's properties using class-based views

萝らか妹 提交于 2019-12-06 13:02:13
Let's say my app is like a forum, but that each post has a group of people which may see it. SecretPost(Model): can_see = myapp.main.models.GroupOfUsers() I want to write a view which restricts users' access to these posts, and I'd prefer to use decorators, since that's how I've been handling access control everywhere else. SecretPostView(DetailView): """Can only be seen by members of its group""" @method_decorator(part_of_its_group) def dispatch(self, request, *args, **kwargs): return super(SecretPostView, self).dispatch(request, *args, **kwargs) But when dispatch() is called, I don't know

Handle PROTECT ERROR in Django DeleteView

感情迁移 提交于 2019-12-06 10:29:41
问题 I am using Django DeleteView to delete items in my database. I have use separate template to show the delete confirmation message, but when I press the yes button I get ProtectedError since customer table is linked with Accounts table. Hence I want to handle the ProtectedError and give user another message in the same template. Here is the code I have used to perform the delete: class Customer(DeleteView): #Delete Customers model = Customer template_name = 'project_templates/delete_customer

Django CreateView: set user before validation

风流意气都作罢 提交于 2019-12-06 09:52:45
I have a model that uses different validation for its name field depending on whether the object was created by a user or by the system. class Symbol(models.Model): name = models.CharField(_('name'), unique=True, max_length=64) creator = models.ForeignKey('User', null=True, on_delete=models.CASCADE) def is_system_internal(self): """ whether or not this Symbol belongs to the system rather than having been created by a user """ return (self.creator is None) def clean(self): """ ensure that the Symbol's name is valid """ if self.is_system_internal(): if not re.match("^_[a-zA-Z0-9\-_]+$", self

django: passing extra argument to the template with generic view class

谁说胖子不能爱 提交于 2019-12-06 09:46:47
I want to use the same template to display records from different models in django, with generic class viewers. The generic class viewers already accept most of the arguments needed in the template, except for one. How can I pass this extra argument in the context to the template? I have tried passing it as the third (extra) argument in the urlconf, without success: # in urlconf.py url(r'^processador/(?P<pk>[\w-]+)/$', UpdateView.as_view( model=Processador, template_name='model_form.html', success_url=reverse_lazy('processador-list'), ), {'extrainfo': "Processador"}, name='processador-detail'

How to apply decorator do dispatch method in class-based views Django

旧街凉风 提交于 2019-12-06 08:29:02
问题 Reading a 'ProDjango' book, I've found interesting moment about applying custom decorator to methods in class-based views. Author says that we can either manually assign decorator to each method of class, i.e., get , post and so on, or we can add our decorator to dispatch() method and if we do so then decorator will be applied to each method of class( get , post etc) Question is: How actually I can apply decorator to dispatch() method of Class-based view? 回答1: You can use the decorator method

Converting a function based view to a class based view with only a form and no model (object)

微笑、不失礼 提交于 2019-12-06 05:16:43
问题 Right now, this is how the password is changed within a user profile. What is the best way of converting this to a class based view knowing that there is no model involved? This is the view for changing the password @login_required def profile_change_password(request): """ Change password of user. """ user = get_object_or_404(User, username__iexact=request.user.username) if request.method == 'POST': form = PasswordChangeFormPrivate(user=user, data=request.POST) if form.is_valid(): form.save()

Django CreateView is not saving object

六月ゝ 毕业季﹏ 提交于 2019-12-06 00:42:41
问题 I'm practicing django Class-Based-View with a basic blog application. For some reason, however, the CreateView for my Post model is not saving the post inside the database. models.py class Post(models.Model): user = models.ForeignKey(User) post_title = models.CharField(max_length=200) post_content = models.CharField(max_length=500) post_date = models.DateTimeField('date posted') forms.py class PostForm(forms.ModelForm): class Meta: model = Post exclude = ('user', 'post_date') views.py class

Django Class Based View: Validate object in dispatch

三世轮回 提交于 2019-12-05 21:35:13
Is there a established way that i validate an object in the dispatch without making an extra database call when self.get_object() is called later in get/post? Here is what i have so far (slightly altered for this question): class CourseUpdateView(UpdateView): def dispatch(self, request, *args, **kwargs): self.request = request self.kwargs = kwargs self.object = self.get_object() if self.object.is_online: messages.warning(request, "Sorry this one can't be updated") return redirect("course:detail", pk=self.kwargs['pk']) # this is going to call self.get_object again isn't it? return UpdateView

How do I paginate WeekArchiveView?

被刻印的时光 ゝ 提交于 2019-12-05 18:38:57
In continuation of my struggle with WeekArchiveView , how do I paginate it by week? All I want is: to know if there is next / previous week available; in case there is, provide a link in the template. I'd like it to also skip empty weeks. The source shows get_next_day / get_prev_day and get_next_month / get_prev_month are available, but nothing for weeks. That is definitely interesting. Sure enough MonthMixin includes get_next_month / get_prev_month methods, and DayMixin includes get_next_day / get_prev_day methods. However, both YearMixin and WeekMixin have no functional equivalent in their

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

ぃ、小莉子 提交于 2019-12-05 13:45:26
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(max_length=255,) email = models.EmailField() My view: class CreateContactView(LoginRequiredMixin,