django-class-based-views

Object ownership validation in Django UpdateView

独自空忆成欢 提交于 2019-12-04 14:21:47
问题 EDIT: The better solution for me was just using a permissions system, especially since I needed other types of controlled access to objects. I now use Django-guardian to help with object level permissions like this. Original: I'm expanding a bit on the standard django book guide by letting users upload stories, as well as having author, publisher, etc. I'm attempting to only let authors (creators) of a story use the updateview, with other users being redirected away. Modifying get_object in

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

天涯浪子 提交于 2019-12-04 14:06:18
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? schillingt You can use the decorator method_decorator as shown here in the docs . From the docs: from django.contrib.auth.decorators import

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

孤街浪徒 提交于 2019-12-04 10:06:29
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() messages.add_message (request, messages.INFO, _('password changed')) return HttpResponseRedirect

Django Call Class based view from another class based view

ぃ、小莉子 提交于 2019-12-04 08:57:36
问题 i am trying to call a class based view and i am able to do it, but for some reason i am not getting the context of the new class that i am calling class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView): template_name = "accounts/thing.html" @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(ShowAppsView, self).dispatch(*args, **kwargs) def get(self, request, username, **kwargs): u = get_object_or_404(User, pk=self.current_user_id(request)) if u

Django Generic Relations error: “cannot resolve keyword 'content_object' into field”

谁都会走 提交于 2019-12-04 07:43:00
I'm using Django's Generic Relations to define Vote model for Question and Answer models. Here is my vote model: models.py class Vote(models.Model): user_voted = models.ForeignKey(MyUser) is_upvote = models.BooleanField(default=True) # Generic foreign key content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') class Meta: unique_together = ('content_type', 'user_voted') views.py user_voted = MyUser.objects.get(id=request.user.id) object_type = request.POST.get('object_type') object = None;

Django CreateView is not saving object

房东的猫 提交于 2019-12-04 06:54:36
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 PostCreate(CreateView): template_name = 'app_blog/post_save_form.html' model = Post form_class =

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

一世执手 提交于 2019-12-04 06:41:43
问题 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 _

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

醉酒当歌 提交于 2019-12-04 04:35:35
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", null=False ) name = models.CharField( blank=False, max_length=58, ) is_active = models.BooleanField

How to validate mulitple forms in a single formView class Django

萝らか妹 提交于 2019-12-03 21:24:57
I have a formView class as you can see below:- view.py class ThreadForm(FormView): template_name = 'thread.html' form_class = ThreadModelForm success_url = '/success' def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. print form.cleaned_data return super(ThreadForm, self).form_valid(form) def get_context_data(self, **kwargs): context = super(ThreadForm, self).get_context_data(**kwargs) context['second_form'] = MessageModelForm return context thread.html {form.as_p} {second_form.as_p} SUBMIT In my template thread.html ,

Django: ListView with post() method?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 14:17:10
I am trying to process two forms in a Django class based view. The site contains a form called form (based on GET ) for narrowing the list results of the ListView and the second form status_form (based on POST ). Both forms are required since the ListView returns a list of items. Form lets the user restrict the choices and status_forms lets the user flag incorrect items via a modal form (therefore it needs to be in the same template). My trouble is that ListView does not come with the method post , however FormView does. My class List inherits from both classes, but when I execute the class I