django-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

ListView and CreateView in one template Django

北慕城南 提交于 2019-12-04 14:09:28
问题 I'm designing a page in which people can view and create objects of a certain sort (the objects are instances of the model Project). As I understand it, I can't do it in one view without horribly messy code, so I am trying to understand how I can use one template to show two views (the ProjectCreateView and the ProjectListView). Right now, this is what I am working with: views.py: class ProjectCreateView(CreateView): model = Project template_name = "fileupload/project_list.html" fields = [

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

Django 1.3 CreateView/ModelForm: unique_together validation with one field excluded from form

孤者浪人 提交于 2019-12-04 14:05:55
I am looking for a simple answer by example to this common problem. The answers I found so far leave out critical points for us beginners. I have an app where almost every model has a ForeignKey to User, and there is a unique_together constraint, where one of the fields is always 'user'. For example: class SubscriberList(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=70) date_created = models.DateTimeField(auto_now_add=True) class Meta: unique_together = ( ('user', 'name',), ) def __unicode__(self): return self.name A SubscriberList is always created by a

Django - Ajax registration

帅比萌擦擦* 提交于 2019-12-04 13:52:09
问题 I am trying to allow registration (using this django-registration register view) to one of my applications from a modal dialog. Since this form is in a modal box, I'd like to get an json reponse on success (instead of the default redirection) How can I use this view (django-registration register) to manage the registration and send back a json response on success ? I know how to make ajax/json responses, the question is how to use the django-registration view without the redirection behavior

Django Import Class From models.py

半世苍凉 提交于 2019-12-04 13:38:58
Using a folder structure like this: library/ -django.wsgi -manage.py -static/ --all my static files -library/ --__init__.py --models.py --settings.py --urls.py --views.py --wsgi.py --templates/ ---where i plan to store all my templates How can i import a class in my views.py that is defined in models.py? I've tried: from . import models.class from models import class from projectname.models import class from projectname import models.class from project import class But for all those i get invalid syntax errors views.py from django.core.context_processors import csrf from django.shortcuts

How to use two different Django Form at the same template?

人走茶凉 提交于 2019-12-04 13:12:21
问题 My forms.py: class AlertForm(forms.ModelForm): class Meta: model=Alert fields = ('high','medium', 'user') widgets = { 'user': forms.HiddenInput() } AlertCountFormset = modelformset_factory(Alert, form = AlertForm) Another Django Form class: class NotifierForm(forms.ModelForm): high = forms.ChoiceField(choices=NOTIFIER_TYPE) medium = forms.ChoiceField(choices=NOTIFIER_TYPE) low = forms.ChoiceField(choices=NOTIFIER_TYPE) def save(self, commit=True): alert = super(NotifierForm, self).save(commit

Advantages of using REST over simple URL and view creation in Django?

瘦欲@ 提交于 2019-12-04 12:29:51
It might be a silly question for many, but why can't I instead Create a view in django that takes a request and returns HttpResponse in, say, JSON format Map the view to a URL Hit the URL from my browser or another server and use the result? Thanks. EDIT - Two approaches: Import some djangorestframework or tastypie and build an api in my application which will throw json responses VS building a class based view and tell it to return json response . Is there any huge advantage of using the first one? I think you could make the same argument about any extension library. It just depends on how

Get the click event from a button in a django view

若如初见. 提交于 2019-12-04 12:19:55
问题 i think the title is pretty clear. I want to know when the user clicks the button to run a piece of code in a function in my views.py. lets say i have this html: <div> <input type="button" name="_mail" value="Enviar Mail"> </div> and i want to run this code if the user clicks on it: send_templated_mail(template_name='receipt', from_email='robot@server.com', recipient_list=[request.user.email], context=extra_context) that´s all i want to do. EDIT : this is the view that i have: def verFactura

What's the best way to create a model object in Django?

て烟熏妆下的殇ゞ 提交于 2019-12-04 11:31:03
问题 Author.objects.create(name="Joe") or an_author = Author(name="Joe") an_author.save() What's the difference between these two? Which one is better? Similar questions: - difference between objects.create() and object.save() in django orm - Django: Difference between save() and create() from transaction perspective 回答1: create() is like a wrapper over save() method. create(**kwargs) A convenience method for creating an object and saving it all in one step Django 1.8 source code for create()