django-generic-views

django 'str' object is not callable

谁说胖子不能爱 提交于 2019-11-26 20:56:49
问题 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

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

亡梦爱人 提交于 2019-11-26 15:35:58
问题 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. ` 回答1: How about overriding form

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

左心房为你撑大大i 提交于 2019-11-26 12:35:05
问题 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? 回答1: If your urlconf looks something like this: url(r'^(?P<slug>[a-zA-Z0-9-]+)/$', MyView