django-class-based-views

How to avoid repeating field list in ModelForm and CreateView class?

ε祈祈猫儿з 提交于 2019-12-11 07:16:18
问题 I'm using django.forms.ModelForm and django.views.generic.CreateView to create a creation view for my model. I find that I end up with this code: forms.py: class ScenarioForm(forms.ModelForm): class Meta: model = Scenario fields = ['scenario_name', 'description', 'scenario_file', 'preview'] views.py: class ScenarioUpload(generic.CreateView): model = Scenario fields = ['scenario_name', 'description', 'scenario_file', 'preview'] form_class = ScenarioForm It seems like really bad repetition. Is

How do I display most recent week items by default with WeekArchiveView?

家住魔仙堡 提交于 2019-12-11 02:38:48
问题 I'm astonished by how little documentation on class-based generic views there is. Anything slightly more complex than a trivial sample has to get done through guesswork, trial and error. I want to use WeekArchiveView to display a week's item list. There's my urls.py entry: url(r'^items/(?P<year>\d{4})/week/(?P<week>\d{1,2})/$', ItemWeekArchiveView.as_view()) When no year or week is specified, I get an error page. I want them to equal today's year and week by default. What is the right place

Class Based View MySQL DateTimeField received a naive datetime

给你一囗甜甜゛ 提交于 2019-12-11 01:48:31
问题 I am very new to Django and at the end of my rope and really need some help. I do not know how to use a "class based view" and change the incoming datetimefield from my MySQL database into a Time Zone Supported entry that it seems to need. The database stores it in UTC and my system is on PST. I am getting this error: DateTimeField received a naive datetime (2012-09-01 00:00:00) while time zone support is active On my MonthArchiveView, DayArchiveView, DateDetailView 's only. For some reason

DatepickerWidget in CreateView

放肆的年华 提交于 2019-12-10 20:12:44
问题 In my model I have a Datefield. So I want to use a Datepicker. How to use the Django-Admin Datepicker? I have found examples to do this in a Form, but I have only desined a model. Is it possible to define this widget in my Model? 回答1: You can use get_form method to override widget attribute: class MyCreateView(CreateView): def get_form(self, form_class): form = super(MyCreateView, self).get_form(form_class) form.fields['date_field'].widget.attrs.update({'class': 'datepicker'}) return form 回答2

How to use updateview with a ForeignKey/OneToOneField

时光总嘲笑我的痴心妄想 提交于 2019-12-10 18:37:16
问题 class ModTool(models.Model): ... issue = models.OneToOneField(Issue) priority = models.CharField(max_length=1, choices=PRIORITY, blank=True) status = models.CharField(max_length=1, choices=STATUS, default='O', blank=True) url url(r'^moderate/(?P<pk>\d+)', ModEdit.as_view(),name='moderation') view class Modedit(UpdateView): model = ModTool template_name = 'myapp/moderate.html' fields = ['priority','status'] At this point I am not able to figure out how to set this view to edit the particular

Render a template instead a success_url in form_valid with FormView django

China☆狼群 提交于 2019-12-10 14:47:15
问题 As the title says: I need to render a template after submitting a form, this form is handled with FormView with the method form_valid . With the method post , I can render a template after submitting it but maybe with form_valid , I can do it in the cleanest way. How can I do it? 回答1: The default implementation of form_valid is to redirect to success_url , you only need to override it to render some page. Here is the example. class ChangePasswordPage(FormView): template_name = 'core/password

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

隐身守侯 提交于 2019-12-09 17:48:41
问题 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

Django - using reverse() on Class-based views

半腔热情 提交于 2019-12-08 18:48:33
I have the following urls configuration in my Django project: urlpatterns = patterns('', (r'^my-view$', MyViewClass.as_view()), ) Is there a way to use the reverse() function to get the url of the above view? Yes there is. Use the name argument of the url function to define a name for the url, then you can use reverse on this name: from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^my-view$', MyViewClass.as_view(), name='my_view'), ) reverse('my_view') 来源: https://stackoverflow.com/questions/25140133/django-using-reverse-on-class-based-views

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 - using reverse() on Class-based views

天涯浪子 提交于 2019-12-08 04:44:35
问题 I have the following urls configuration in my Django project: urlpatterns = patterns('', (r'^my-view$', MyViewClass.as_view()), ) Is there a way to use the reverse() function to get the url of the above view? 回答1: Yes there is. Use the name argument of the url function to define a name for the url, then you can use reverse on this name: from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^my-view$', MyViewClass.as_view(), name='my_view'), ) reverse('my_view') 来源: https