django-class-based-views

How to Access Many to many field in class based views in Django?

本秂侑毒 提交于 2019-12-23 23:02:16
问题 I have two models, Author & Book in models.py class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField() age = models.IntegerField() def __str__(self): return '%s %s' %(self.first_name, self.last_name) def __unicode__(self): return '%s %s' %(self.first_name, self.last_name) class Book(models.Model): title = models.CharField(max_length=100) #name = title pages = models.IntegerField() price = models

I can't update Django's RedirectView. It keeps referring to the old URL with a status of 301 Moved Permenantly

喜欢而已 提交于 2019-12-23 10:09:56
问题 Sorry if this question was supposed to be in Server Vault. I couldn't really tell whether it's a programming error or a server configuration error. I recently pushed my git commits to the live server and I noticed something very frustrating. No matter how I edit the urls.py , I can't seem to update RedirectView ! Here is my root mysite/urls.py urlpatterns = patterns('', url(r'^$', RedirectView.as_view(url=reverse_lazy('order_list')), name='home'), url(r'^doors/', include('doors.urls')), url(r

Access request.user in Django Classed Based Generic View

▼魔方 西西 提交于 2019-12-23 08:03:59
问题 How can I access request.user inside a Classed Based Generic View? This is the code: class TodayView(TemplateView): template_name = "times/today.html" if Records.objects.all().count() > 0: last_record = Records.objects.latest('id') else: last_record = None actual_time = datetime.today() activities = Activity.objects.filter(owner=request.user) def get_context_data(self, **kwargs): context = super(TodayView, self).get_context_data(**kwargs) context["today"] = self.actual_time return context

Access request.user in Django Classed Based Generic View

早过忘川 提交于 2019-12-23 08:01:06
问题 How can I access request.user inside a Classed Based Generic View? This is the code: class TodayView(TemplateView): template_name = "times/today.html" if Records.objects.all().count() > 0: last_record = Records.objects.latest('id') else: last_record = None actual_time = datetime.today() activities = Activity.objects.filter(owner=request.user) def get_context_data(self, **kwargs): context = super(TodayView, self).get_context_data(**kwargs) context["today"] = self.actual_time return context

How to add headers and footers with django-wkhtmltopdf in my class based views with PDFTemplateResponse

我的梦境 提交于 2019-12-23 04:58:07
问题 I want to generate a pdf with headers, actually I'm working on cygwin andI don't know if something in my code is wrong or not, because I based my code in this examples Creating PDFs with django (wkhtmltopdf). This is my code: views.py from django.views.generic import View from wkhtmltopdf.views import PDFTemplateResponse GenerateReportPdf(View): def __init__(self): self.template = 'pdf_template.html' def get(self, request): ... response = PDFTemplateResponse( request=request, template=self

Django Using Slug Field for Detail URL

只谈情不闲聊 提交于 2019-12-23 03:26:48
问题 I'm attempting to setup my site so that the url for my job-detail will use a slug field instead of a pk. It's telling me that it cannot find my job with the given slug (which is an int, 147). Update: After looking at the DetailView description at https://ccbv.co.uk/projects/Django/1.11/django.views.generic.detail/DetailView/ I realized there is a slug_field attribute for DetailView . My new view looks like: class JobDetailView(CacheMixin, DetailView): model = Job slug_field = 'slug' Question:

Using class-based UpdateView on a m-t-m with an intermediary model

╄→尐↘猪︶ㄣ 提交于 2019-12-23 01:19:05
问题 How can I convince a Django 1.3 class based generic view: UpdateView.as_view(model=Category, template_name='generic_form.html', success_url='/category/') To not give up so easy with error: "Cannot set values on a ManyToManyField which specifies an intermediary model." Even if all fields in the intermediary model have defaults, I can't get the class based generic view to save. The functional based version looks messy also. Django 1.3. 回答1: You should extend UpdateView and override the form

Using Multiple ModelForms with Class-Based Views

对着背影说爱祢 提交于 2019-12-23 00:48:34
问题 I've got a situation where I'd like to add an additional modelform to my CreateView. We have an entry order system that allows someone to add an order and then add items to that order. Typically, when someone adds an order for the first time they'd like to also add an item to that order, so I want to combine those models into a single form and process them on initial order entry. I'm running into a problem when the forms don't validate. I've overridden get_context_data to add the item form to

'str' object has no attribute 'visible_fields'; django class based views

♀尐吖头ヾ 提交于 2019-12-22 13:57:10
问题 I've been struggling with class views all day after starting on them yesterday. My issue is constantly getting 'str' object has no attribute 'visible_fields' , so the 'form' item below is not really a form: template- <form action="" method="post"> {% csrf_token %} {{form|bootstrap}} <input type="submit" name="submit" value="Add new article"/> </form> view- class ArticleCreateView(CreateView): model = Article template_name = 'index/add_article.html' form_class = ArticleForm def post(self,

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

回眸只為那壹抹淺笑 提交于 2019-12-22 12:44:09
问题 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',