django-generic-views

How do I set initial data on a Django class based generic createview with request data

微笑、不失礼 提交于 2019-11-29 02:10:16
I used Django's generic createview for my model from myproject.app.forms import PersonForm class PersonMixin(object): model = Person form_class = PersontForm class PersonCreateView(PersonMixin, CreateView): pass This works perfectly for displaying a create view of Person with my custom form. However, I have a field in the form that I want to pre-populate with a value. I found this answer: Set initial value to modelform in class based generic views However, my pre-populated value comes from the profile for request.user. How do I access the request in PersonCreateView and pass that to the form?

Django: How to login user directly after registration using generic CreateView

◇◆丶佛笑我妖孽 提交于 2019-11-28 21:43:15
With django generic CreateView I can create a new user account, but how can I login this user automatically after registration using this technique? urls.py ... url( r'^signup/$', SignUpView.as_view(), name = 'user_signup' ), ... views.py class SignUpView ( CreateView ) : form_class = AccountCreationForm template_name = 'accounts/signup.html' success_url = reverse_lazy( 'home' ) forms.py class AccountCreationForm ( forms.ModelForm ) : def __init__( self, *args, **kwargs ) : super( AccountCreationForm, self ).__init__( *args, **kwargs ) for field in self.fields : self.fields[field].widget.attrs

Use get_queryset() method or set queryset variable?

六眼飞鱼酱① 提交于 2019-11-28 16:26:05
问题 These two pieces of code are identical at the first blush: class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_poll_list' queryset = Poll.active.order_by('-pub_date')[:5] and class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_poll_list' def get_queryset(self): return Poll.active.order_by('-pub_date')[:5] Is there any difference between them? And if it is: What approach is better? Or when setting

Excluding fields in generic CRUD views

被刻印的时光 ゝ 提交于 2019-11-28 10:57:09
问题 I have a model named Domain which looks like this: class Domain(models.Model): """ Model for storing the company domains """ user = models.ForeignKey( User ) host = models.CharField( null=False, verbose_name="Host", max_length=128, unique=True ) I'd like to use Django's generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn't need any user input. How can I exclude that field from the form that my generic view

Redirecting after AJAX post in Django

◇◆丶佛笑我妖孽 提交于 2019-11-28 05:27:05
I use Django's built-in DeleteView and I've assigned a value to the success_url attribute. Now in my template, I trigger this view via JQuery' $.post() method. When the item is deleted, I don't get redirected to the success_url . After some search, I found that it seems to be a problem of AJAX post method, which ignores the redirection. I fixed it by adding a function to set the window.location="#myRedirectionURL" as the third parameter of $.post() in JQuery. However, this way seems not very Django. Essentially, it solves the problem from the aspect of AJAX, instead of Django. What's more, it

django 'str' object is not callable

﹥>﹥吖頭↗ 提交于 2019-11-27 22:14:35
I have a problem creating an URL view in django. It gives me this error (ferrol is a Space object): TypeError at /spaces/ferrol/ 'str' object is not callable Request Method: GET Request URL: http://localhost:8000/spaces/ferrol/ Django Version: 1.2.3 Exception Type: TypeError Exception Value: 'str' object is not callable Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg/django/core/handlers/base.py in get_response, line 100 Here is the code: spaces/models.py class Space(models.Model): """ Basic spaces model. """ name = models.CharField(_('Name'), max_length=100,

How do I set initial data on a Django class based generic createview with request data

早过忘川 提交于 2019-11-27 15:07:35
问题 I used Django's generic createview for my model from myproject.app.forms import PersonForm class PersonMixin(object): model = Person form_class = PersontForm class PersonCreateView(PersonMixin, CreateView): pass This works perfectly for displaying a create view of Person with my custom form. However, I have a field in the form that I want to pre-populate with a value. I found this answer: Set initial value to modelform in class based generic views However, my pre-populated value comes from

Accessing request.user in class based generic view CreateView in order to set FK field in Django

十年热恋 提交于 2019-11-27 11:41:32
So I have a model that includes: class Place(models.Model): .... created_by = models.ForeignKey(User) My view is like so: class PlaceFormView(CreateView): form_class = PlaceForm @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(PlaceFormView, self).dispatch(*args, **kwargs) Is there a way for me to access request.user and set created_by to that user? I've looked through the docs, but can't seem to find any hints toward this. ` How about overriding form_valid which does the form saving? Save it yourself, do whatever you want to it, then do the redirect. class

Redirecting after AJAX post in Django

為{幸葍}努か 提交于 2019-11-27 05:30:38
问题 I use Django's built-in DeleteView and I've assigned a value to the success_url attribute. Now in my template, I trigger this view via JQuery' $.post() method. When the item is deleted, I don't get redirected to the success_url . After some search, I found that it seems to be a problem of AJAX post method, which ignores the redirection. I fixed it by adding a function to set the window.location="#myRedirectionURL" as the third parameter of $.post() in JQuery. However, this way seems not very

Django class-based view: How do I pass additional parameters to the as_view method?

我的梦境 提交于 2019-11-27 02:44:53
I have a custom class-based view # myapp/views.py from django.views.generic import * class MyView(DetailView): template_name = 'detail.html' model = MyModel def get_object(self, queryset=None): return queryset.get(slug=self.slug) I want to pass in the slug parameter (or other parameters to the view) like this MyView.as_view(slug='hello_world') Do I need to override any methods to be able to do this? If your urlconf looks something like this: url(r'^(?P<slug>[a-zA-Z0-9-]+)/$', MyView.as_view(), name = 'my_named_view') then the slug will be available inside your view functions (such as 'get