django-generic-views

Django 1.6, how to set field default value for CreateView

淺唱寂寞╮ 提交于 2019-12-11 05:58:38
问题 models.py class App(models.Model): name = models.CharField(max_length=10, default='') desc = models.CharField(max_length=10, default='') views.py class AppCreate(CreateView): template_name = "app/add.html" model = App templates <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="" method="POST"> {% csrf_token %} {{form}} <input type="submit" value='submit'> </form> </body> </html> Here two problems: Why I set model field default value,

Django Create and Update views: foreign key field

天大地大妈咪最大 提交于 2019-12-11 05:11:37
问题 Suppose I have the two following classes : class Parcel(models.Model): name = models.CharField(max_length=NAME_MAX_LENGTH) garden = models.ForeignKey(Garden, on_delete=models.CASCADE) def __str__(self): return self.name class Bed(models.Model): parcel = models.ForeignKey(Parcel, on_delete=models.CASCADE) name = models.CharField(max_length=NAME_MAX_LENGTH) length = models.IntegerField() width = models.IntegerField() I'm using Django's generic views to Create and Update new beds. As parcel is a

Rolling your own generic views in Django

怎甘沉沦 提交于 2019-12-11 01:47:32
问题 The Django documentation mentions in the Class-based generic views that the DetailView is composed from: View, SingleObjectMixin, and SingleObjectTemplateResponseMixin. I am experimenting with this, as I am interested in creating a generic view that will do an object_detail view with a ModelForm so that my model rows can be generated automatically. To try to duplicate the DetailView I tried to create a class as follows: from django.views.generic import list_detail, View from django.views

Django: UpdateView restrict per user

喜欢而已 提交于 2019-12-10 13:27:20
问题 I have a site where users can create and edit their own lists. I'm using the generic view CreateView to allow users to create lists. I would like to use the generic view UpdateView to allow them to edit the lists, but the login_required=True is not enough in this case, since only the list creator can edit his/her list. 2 questions: 1) is there any parameter that I can specify in the URLconf to add this restrictions? 2) can I impose the those generic views should only work with POST and not

Can I make a view using UpdateView and DeleteView together?

梦想的初衷 提交于 2019-12-10 11:55:49
问题 I am looking for some information about generic views in django. I want a page that will render a form for an object. On my page I want one submit button to update the object and another submit button that will delete the object. Is it a possible to do this using a single view? 回答1: Yes this is possible. It is possible create a view to handle update and delete functionality using both class based views and function based views. You will find it much easier using function based views. (I'm not

Django - CreateView - How to declare variable and use it in templates

℡╲_俬逩灬. 提交于 2019-12-10 03:57:51
问题 How do I declare a variable in Django's Createview, so I can use it from its template? For example I want to use {{ place_slug }} in the template. I pass that from urls.py like below: urls.py: urlpatterns = patterns('', (r'^new/(?P<place_slug>[\w\-\_]+)/?$', PictureCreateView.as_view(), {}, 'upload-new'), ) views.py: class PictureCreateView(CreateView): model = Picture def dispatch(self, *args, **kwargs): self.place = get_object_or_404(Place, slug=kwargs['place_slug']) return super

Django: How to set a hidden field on a generic create view?

天大地大妈咪最大 提交于 2019-12-09 00:51:57
问题 I'm running Django 1.6.x To extend my user I've added another model storing the data: class UserProfile (models.Model): user = models.ForeignKey(User) height = models.IntegerField(blank=True, null=True) Now I wand to add a view, which is allowing the user to add its own infos there. Started with django.views.generic.edit.CreateView , but want also to provide at least the edit/update one to. So I've added the import and created a view: from django.views.generic.edit import CreateView,

AttributeError: myview has no attribute object in custom mixin in Django

≡放荡痞女 提交于 2019-12-08 08:10:18
问题 I am trying to write a mixin for able to save a form partially and resume later. This is useful when the form is long and user cannot finish in one-sitting. The mixin code below comes directly from prodjango book by Marty Alchin. I have commented in the code where the error comes which is the POST method in mixin. Detailed error description below. From the traceback, I think the error comes from these two calls self.get_form(form_class) and get_form_kwargs . but I have no idea how to fix this

django how to loop through the context object passed back by a generic detailview?

狂风中的少年 提交于 2019-12-06 11:01:14
问题 I'm using a generic DetailView to display a project object. Can I loop through the fields somehow in my template or do I have to place every field. url(r'^(?P<slug>[-\w]+)/$', DetailView.as_view(model=Project, template_name='projects/detail_project.html',slug_field='slug', context_object_name='project'), name='project_detail'), I've got something like this in my template: {{ project.title }} {{ project.created_date }} etc... Is there a way to do something like this? <table> {% for field in

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