I am generating an object from a submitted form. I want to pass along the generated AutoField to the next form as a hidden form element. However, I cannot figure out how to pass a variable from post() to get_context_data(). I know that post() is called first, followed by get_context_data(), but adding the variable to self does not yield the results I expect (the attribute does not exist on self).
Here is an example of what I am experiencing:
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
print self.hello
return context
def post(self, request, *args, **kwargs):
self.hello = "hello"
return HttpResponseRedirect(request.path)
self.hello is not valid when called in get_context_data. I feel as though I may be mistaken with the HttpResponseRedirect(request.path) call, but I'm not sure how else to render the template.
Is it a simple mistake, or should I be approaching this an entirely different way?
By defining post yourself, you've overridden the default behaviour of the view. You can see that there is no call to get_context_data, or any of the other class methods, so naturally they won't be called.
Generally you should not be overriding the specific get or post methods. You haven't shown the full view so it's not clear what behaviour you are trying to achieve, but for example in a form view you would want to define success_url to set the place the form redirects to after submission.
You have to find a way to pass the object ID to the next page. The options that come to mind are to put it into the URL or as solarissmoke has suggested save it in the session. If you are doing it in the url you can also put the page sequence there (meaning 1 for the forst form, 2 for the second...).
The nice thing about this approach is, that you can cover all functonailty in one view: depending on the page set the respective fields in the get_object methods (self.fields=[....]) and the template names in the get_template_names method.
So using an Updateview, it would look like this:
urls.py:
....
url(r'^mysite/(?P<object_no>\d+)/(?P<form_no>\d+)$', BaseView.as_view()),
views.py:
class BaseView(UpdateView):
def get_object(self):
obj=MyModel.objects.get(id=self.kwargs['object_no'])
form_no = self.kwargs['form_no']
if form_no=="1":
self_fields=["field1","field2"...]
.....
def get_object(self):
obj=MyModel.objects.get(id=self.kwargs['object_no'])
form_no = self.kwargs['form_no']
if form_no=="1":
self_fields=["field1","field2"...]
.....
return obj
def get_template_names(self):
from_no = self.kwargs['form_no']
if form_no=="1":
return ["template1.html"]
....
You have to make sure that all your fields can be null.
Related
I have a CBV based on a TemplateView
It has a post(self) method to access user input.
Also, it has a template_name property which should be populated with data from post().
No Django models or forms are used here.
Attempt to extract data within post method:
# views.py
class ReturnCustomTemplateView(TemplateView):
def post(self, *args, **kwargs):
chosen_tmplt_nm = self.request.POST.get('tmplt_name')
print(chosen_tmplt_nm) # correct value.
# how do I use it outside this method? (like bellow)
template_name = chosen_tmplt_nm
... or is there any other way I can get the data from request.POST without def post()?
You can override the get_template_names() method [Django-doc] which returns an iterable (for example a list) of template names to search for when determining the name of the template, so:
class ReturnCustomTemplateView(TemplateView):
def get_template_names(self):
return [self.request.POST.get('tmplt_name')]
def post(self, *args, **kwargs):
return self.get(*args, **kwargs)
I would however advise to be careful with this: a POST request could be forged, so a hacker could make a POST request with a different template name, and thus try to obtain for example the content of the settings.py file or another file that contains sensitive data.
I have a listview that I access in a pretty bog standard way to return all metaobjects.
#url
url(r'^metaobject/$', MetaObjectList.as_view(),name='metaobject_list'),
#ListView
class MetaObjectList(ListView):
model = MetaObject
I've recently added a search form that I want to scan my objects (I've got about 5 fields but I've simplified the example). What I'd like to do is re-use my MetaObjectList class view with my specific subset. I am guessing I need to override the get_queryset method but I'm not clear in how I get the queryset from my FormView into the listview. I mucked around a bit with calling the as_view() in the formveiw's form_valid function with additional parameters but couldn't get it to work and it seemed hacky anyway.
class SearchView(FormView):
template_name = 'heavy/search.html'
form_class = SearchForm
#success_url = '/thanks/'
def form_valid(self, form):
#build a queryset based on form
searchval=form.cleaned_data['search']
list = MetaObject.objects.filter(val=search)
#where to from here?
I also looked at trying to post the data from the form view over to the listview but that seemed like I'd need to re-write the form logic into the listview.
I'm on python 3.x and django 1.11.
I found what I feel is more elegant than the comment on the question:
My form valid now points to the list object's as_view method and passes the request and the queryset I want
def form_valid(self, form):
#build a queryset based on form
searchval=form.cleaned_data['search']
list = MetaObject.objects.filter(val=search)
return MetaObjectList.as_view()(self.request,list)
This hits the ListView as a post which I use to alter the queryset
class MetaObjectList(ListView):
model = MetaObject
queryset = MetaObject.objects.prefetch_related('object_type','domain')
def post(self, request, *args, **kwargs):
self.queryset = args[0]
return self.get(request, *args, **kwargs)
The only obvious change is using kwargs to make it a bit clearer. Otherwise this seems to work well.
I want to prevent logged-in users to access login and register forms.
I've build custom mixin, but it isn't working. The problem is that even if the user is logged in, he can access login and register forms instead of beeing redirected to homepage.
My Mixin
class MustBeAnonymousMixin(object):
''' Only anonymous users (not logged in) may access login and register
'''
def dispath(self, *args, **kwargs):
if not self.request.user.is_anonymous:
return redirect(reverse('homepage'))
return super(MustBeAnonymousMixin, self).dispatch(*args, **kwargs)
LoginFormView
class LoginFormView(MustBeAnonymousMixin, TemplateView):
'''
Display basic user login form
'''
template_name = 'members/login.html'
def get_context_data(self, **kwargs):
context = super(LoginFormView, self).get_context_data(**kwargs)
context['login_form'] = UserLoginForm()
return context
I'm using Django 1.8. What am I doing wrong?
For another case where mixin does not work:
Remember: "Mixin param" must stand before "GenericView param"
Correct:
class PostDelete(LoginRequiredMixin, generic.DeleteView):
Incorrect:
class PostDelete(generic.DeleteView, LoginRequiredMixin):
Fix the typo in dispath and use is_authenticated() instead of is_anonymous (as indicated in the previous answer already)
is_anonymous should be a function call, and you probably should not use it:
is_anonymous()
Always returns False. This is a way of differentiating User and
AnonymousUser objects. Generally, you should prefer using is_authenticated() to this method.
What is the "correct" way on handle errors inside a custom form_valid() function?
E.g. I'm having a view which is having this form_valid() method defined:
class StorageItemMergeView(FormView):
# ...
def form_valid(self, form):
si = StorageItem.objects.get(pk=self.kwargs["pk"])
si.part.merge_storage_items(si, StorageItem.objects.get(
pk=self.request.POST["storageitem1"]
)
)
return super(StorageItemMergeView, self).form_valid(form)
The form for this is quiet minimal:
class MergeStorageItemsForm(forms.Form):
storageitem1 = forms.ModelChoiceField(queryset=StorageItem.objects.all())
As a matter of fact the user might fill in syntactical correct data which are failing at call of merge_storage_items().
In my case merge_storage_items() is able to return True or False -- but how to react correctly in case of the method call is failing?
The correct way is not to do it in form_valid at all. That's not what it's for. That method is called one the form has been validated and is responsible for saving and redirecting.
The right place to do this is in the form class itself, where you would override the clean method and raise forms.ValidationError if the check fails.
I'm trying to write an app for Django. I want my users to be able to collect certain types of data, for instance samples, videos, etc... The app is called collector and for each type of item there is a class and a form that goes along with it.
Example Class:
class CreateTextView(CreateItemView):
form_class = TextForm
model = Text
def get_context_data(self, **kwargs):
context = super(CreateTextView, self).get_context_data(**kwargs)
context['item_type'] = 'text'
return context
Example Form:
class TextForm(forms.ModelForm):
class Meta:
model = Text
fields = COMMON_FIELDS + ('text',)
As you can see, the actual view is inheriting from CreateItemView. I want as much of the functionality to be defined for CreateItemView so that I don't have to do it individually for all item classes. That has been working for the most part, but it gets a bit tricky when I try to process forms with data.
def post(self, request, *args, **kwargs):
form = TextForm(request.POST) # line 2
form = getattr(TextForm, '__init__')(data=request.POST) # line 3
if form.is_valid():
# Add owner information.
item = form.save(commit=False)
item.owner = request.user
item.save()
return HttpResponseRedirect(reverse('collector:index'))
return render(request, self.template_name, {'form': form})
In Line 2 you can see how I would handle the form if there was only one type of form. Line 3 is what I'm trying to do. I want to be able to use the context['item_type'] to dynamically choose the right form and instantiate it with the given data.
Now the problem lies with the __init__-method which I have never defined anywhere. When I pass only POST.request to __init__, it complains about not having a self. When I pass the additional self, it complains about how CreateTextView has no _meta-attribute and so on. I just can't find the right combination of argumentes to satisfy the __init__-method. I can't look up it's definition either, because I didn't define it. I then followed the definition of the parent classes in the django framework which led me to a couple of complex functions that looked like factories. That didn't really help me...
Now I know how to use the TextForm()-initiation. Is there a way to fetch this method dynamically with getattr()? That would save me the hassle with __init__. If not, how do I provide __init__ with the correct self-instance?
As mentioned below, I have changed my classes a little bit. I no longer use context to store the item_type, instead I use a class variable to have easy acces to the item_type within a view. My post method is defined in the mother class CreateItemView and looks like this now:
def post(self, request, *args, **kwargs):
try:
form_cls = ITEM_TYPE_MAP[self.item_type]
except KeyError:
# invalid item_type. raise a meaningful error here
raise Exception('Invalid item type.')
form = form_cls(request.POST)
if form.is_valid():
# Add owner information.
item = form.save(commit=False)
item.owner = request.user
item.save()
return HttpResponseRedirect(reverse('collector:index'))
return render(request, self.template_name, {'form': form})
A clean and quite simple solution to look for is using a dictionary to map the item_type values to actual form classes:
ITEM_TYPE_MAP = {
"foo": TextForm,
"bar": SomeOtherForm,
}
You’d put that dictionary at some global place and use it from within the controller like this:
item_type = context['item_type']
try:
form_cls = ITEM_TYPE_MAP[item_type]
except KeyError:
# invalid item_type. raise a meaningful error here
raise
form = form_cls(request.POST)
You cannot directly call __init__ usually, because there’s more than that to instanciate an object. Python will also call __new__ on the class of the object, so the only way to be sure is to go through the actual constructor, which is calling the type.
This is what happens above, by first fetching the type into form_cls and then calling the type (i.e. the constructor).