django-class-based-views

Django Class Based View for both Create and Update

倾然丶 夕夏残阳落幕 提交于 2019-12-31 09:11:50
问题 Say I want to create a Class Based View which both updates and creates an object. From a previous question I worked out I could do one of the following things: 1) Use 2 generic views CreateView and UpdateView which I think would mean having two URL's pointing to two different classes. 2) Use a class based view which inherits base View , which I think would mean having two URL's pointing to just 1 class (I created which inherits View ). I have two questions: a) Which is better? b) ccbv.co.uk

post method in generic class based view is not called upon form submission in Django?

China☆狼群 提交于 2019-12-31 04:54:11
问题 I have a written a mixin that overrides the POST and get_from_kwargs of CreateView . I am doing AJAX submission of my form. I see that get_from_kwargs is called by printing on the console. But none of the other methods such as post , form_valid or form_invalid is being called. I have placed print statements in these methods but none of them is being called. Here is my mixin: class PendFormMixin(object): form_hash_name = 'form_hash' pend_button_name = 'pend' def get_form_kwargs(self): """

How to subclass django's generic CreateView with initial data?

守給你的承諾、 提交于 2019-12-29 03:12:26
问题 I'm trying to create a dialog which uses jquery's .load() function to slurp in a rendered django form. The .load function is passed the pk of the "alert" object. Also available in the class functions are things like self.request.user so I can pre-fill those fields, shown below in the Message model (models.py): class Message(models.Model): user = models.ForeignKey(User) alert = models.ForeignKey(Alert) date = models.DateTimeField() message = models.TextField() Subclassing django's CreateView

Sending request.user object to ModelForm from class based generic view in Django

[亡魂溺海] 提交于 2019-12-28 05:33:10
问题 So, my goal is to be able to filter a ModelChoiceField queryset in my ModelForm to only include Places that request.user has created. My ModelForm is simply: class PlaceEventForm(models.ModelForm): class Meta: model = Event I'd like to be able to add something like: def __init__(self, *args, **kwargs): super(PlaceEventForm, self).__init__(*args, **kwargs) self.fields['place'].queryset = Place.objects.filter(created_by=request.user) However, I can't seem to find a way to access the request in

django class based view returns empty string when POST

大兔子大兔子 提交于 2019-12-25 03:49:25
问题 To demostrate: from django.views.generic.base import View from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator class TestView(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return HttpResponse('haha') urls.py is url(r'^test/', TestView.as_view()), so when GET you can see haha , but when doing POST you get a blank page... What am I missing here? Edit: To clarify what I am doing. I am writing a JSON stream

How to render different data to the same page by two different views - Django

安稳与你 提交于 2019-12-24 23:31:07
问题 I'm building a news website.I need display 48 hours most viewed news, this part is in the detail.html page. Now I'm using this method. def newsDetailView(request, news_pk): news = get_object_or_404(News, id=news_pk) News.objects.filter(id=news_pk).update(pv=F('pv') + 1) time_period = datetime.now() - timedelta(hours=48) host_news=news.objects.filter(date_created__gte=time_period).order_by('-pv')[:7] return render(request, "news_detail.html", { 'news': news, 'host_news' : host_news }) It works

NoReverseMatch django Class based views

﹥>﹥吖頭↗ 提交于 2019-12-24 13:33:28
问题 I get this error NoReverseMatch at /author/add/4 Reverse for 'author_update' with arguments '()' and keyword arguments '{'pk': 39, 'user_id': >}' not found. 1 pattern(s) tried: ['author/(?P\d+)/(?P\d+)$'] urls.py url(r'^author/add/(?P<user_id>\d+)$', AuthorCreate.as_view(), name='author_add'), url(r'^author/(?P<user_id>\d+)/(?P<pk>\d+)$', AuthorUpdate.as_view(), name='author_update'), url(r'^author/(?P<user_id>\d+)/(?P<pk>\d+)/delete/$', AuthorDelete.as_view(), name='author_delete'), url(r'

Django UpdateView generic class

。_饼干妹妹 提交于 2019-12-24 06:17:14
问题 How can I access the object passed by the user inside a generic view class? In template, when the user clicks the link: <td><a href="{% url 'update_peon' pk=item.pk %}"><button class="btn btn-warning">Edit</button></a></td> this goes to urls.py: url(r'^update_peon/(?P<pk>\d+)$', views.UpdatePeon.as_view(), name='update_peon'), and my view: class UpdatePeon(generic.UpdateView): login_required = True template_name = 'appform/Peons/peon_form.html' model = Person form_class = PersonForm success

Django: Class based view can't render crispy form

耗尽温柔 提交于 2019-12-24 01:00:01
问题 Hi Stackoverflow people, I have trouble to render a crispy form with a class based view. Everything worked fine when I used the function based views. As usual I generate forms.py as follows: from django import forms from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field from item.models import Item class CreateItemForm(forms.ModelForm): class Meta: model = Item exclude = ('user',) def __init__(self, *args, **kwargs): self

NOT NULL constraint failed Django CreateView

不打扰是莪最后的温柔 提交于 2019-12-23 23:51:44
问题 I would like to accomplish two goals on this project. First, I would like to save the logged-in user as the reviewer. Second, I want to pass the lawyer value from the foreignkey into the review form. (This way, the user can review click on a review link on the lawyer's detail page and review that lawyer and no one else.) I keep getting on the first goal, with this error. error IntegrityError at /lawyers/karlyn-rosemarie-hylton/review/ NOT NULL constraint failed: lawyers_review.reviewer_id Can