Django - FormView combined with Mixin - django

I have to specify a success_url, otherwise I get an error. So how to specify it, in order to stay to the same page?
Also, is everything else correct regarding the SearchView, beucase I have a feeling that something is missing. My context should be composed by form, query, concepts, language and languages.
Thanks
urls.py
url(r'^(?P<langcode>[a-zA-Z-]+)/search/$', SearchView.as_view(), name='search').
views.py
class _LanguageMixin(object):
def dispatch(self, request, *args, **kwargs):
self.langcode = kwargs.pop("langcode")
self.language = get_object_or_404(Language, pk=self.langcode)
return super(_LanguageMixin, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(_LanguageMixin, self).get_context_data(**kwargs)
context.update({"language": self.language,
"languages": Language.objects.values_list('code',
flat=True)})
return context
class SearchView(_LanguageMixin, FormView):
template_name = "search.html"
form_class = SearchForm
success_url = #......
query = ''
concepts = []
def get_initial(self):
return {'langcode': self.langcode}
def get_context_data(self, **kwargs):
context = super(SearchView, self).get_context_data(**kwargs)
context.update({"query": self.query, "concepts": self.concepts})
return context
def form_valid(self, form):
self.query = form.cleaned_data['query']
self.concepts = # here is a long DB query; function(query)
return super(SearchView, self).form_valid(form)
[EDIT]
I did this:
def get_success_url(self):
return reverse('search', kwargs={'langcode': self.langcode})+"?query={}".format(self.query)
The form renders, but whenever I search for anything, I get back the empty search text field. And the URL looks something like this: http://localhost:8000/en-US/search/?query=asd

By default, a FormView (actually, any subclass of ProcessFormView) will return a HttpResponseRedirect in form_valid. As you are calling the super's method in your form_valid method, you also return a HttpResponseRedirect. In the process, the actual POST data is lost, and though you pass it as a GET parameter, it is not used in the actual form.
To fix this, you need to not call super in your form_valid method, but instead return a rendered template in a HttpResponse object, e.g.:
def form_valid(self, form):
self.query = form.cleaned_data['query']
self.concepts = # here is a long DB query; function(query)
return self.render_to_response(self.get_context_data(form=form))

Related

Attibutes are 'disappearing' from CBV. Django

I am trying to build CBV with class View parent. This view takes slug of object and find that object between two django models. The functions from services.py was doing a lot of DB queries, so, I tried to reduce them by giving to FeedbackSection necessary attributes(slug, model_instance and context) and lately override them in get method.
class FeedbackSection(View):
"""
Feedback section for different objects.
This is something like 'generic' view, so I implement it that it will find
the model and feedbacks for this model by having only slug.
"""
template_name = 'feedbacks/feedback-section.html'
form_class = CreateFeedbackForm
slug = None
model_instance = None
context = None
def get(self, request, *args, **kwargs):
self.slug = kwargs.get('slug')
self.model_instance = get_model_instance(self.slug)
self.context = get_feedback_section_context(self.slug, self.form_class, self.model_instance)
return render(request, self.template_name, self.context)
#method_decorator(login_required)
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# will create feedback object and update model[Advert, Company] rating.
end_feedback_post_logic(self.request.user, form, self.model_instance)
return render(request, self.template_name, self.context)
The attributes(slug, model_instance and context), when post method is in runtime are equivalent to None.
The problem is that this implementation was working fine yesterday, but today it's not.
I know I can use my functions again, but in post method. I don't want to do this. Because it will multiple DB Queries by two.
We need to override the setup method of the View class and define those attributes there.
def setup(self, request, *args, **kwargs):
self.slug = kwargs.get('slug')
self.model_instance = get_model_instance(self.slug)
self.context = get_feedback_section_context(
self.slug,
self.form_class,
self.model_instance
)
return super().setup(request, *args, **kwargs)

Django - dynamic success_url in UpdateView

I want my user to be able to see a page, update that page, and then be returned to that page or continue making more edits.
Here's the view to show the information:
# urls.py
url(r'^gameview/$', views.GameView.as_view(template_name='matchview.html'), name='GameView')
# Views.py
class GameView(generic.TemplateView):
template_name = "matchview.html"
def get_context_data(self, **kwargs):
context = super(GameView, self).get_context_data(**kwargs)
q = self.request.GET.get('match')
context['report'] = GameNotes.objects.filter(game=q)
context['game'] = Schedule.objects.get(match=q)
context['gamepic'] = Photo.objects.filter(game=q)
return context
So now they want to add information about a game. I use an UpdateView
class GameView(generic.TemplateView):
template_name = "matchview.html"
def get_context_data(self, **kwargs):
context = super(GameView, self).get_context_data(**kwargs)
q = self.request.GET.get('match')
context['report'] = GameNotes.objects.filter(game=q)
context['game'] = Schedule.objects.get(match=q)
context['gamepic'] = Photo.objects.filter(game=q)
return context
When the user finishes updating in the latter view, they should be returned to the former on the exact same team. This post helped me move in the right direction insofar as using 'get_success_url', but I'm still stuck because I don't think I'm using kwargs. Everything I've tried has resulted in errors.
(My (gulp) thought is that I should re-write the urls to use PKs so that this stuff is easier, but I wanted to make sure)
EDIT:
My fugly attempt (Note: I Have two submit buttons, one to update and one to update and add notes).
def form_valid(self, form):
if form.is_valid():
form.save()
if 'submit' in self.request.POST:
q = self.request.GET.get('match')
return reverse_lazy('TeamView', args=(q))
else:
return render('addnotes', {'game' : q})
SOLUTION:
Learned how to use URL Parameters and kwargs:
(for anyone new like me, self.kwargs.get is brilliant)
def get_success_url(self, **kwargs):
q = self.kwargs.get('match')
if "submit" in self.request.POST:
url = reverse('GameView', args={q : 'match'})
else:
url = reverse('AddNotes', args={q : 'match'})
return url
What about get_absolute_url for the model object?
https://docs.djangoproject.com/en/1.10/ref/models/instances/#get-absolute-url
from django.urls import reverse
class GameModel(models.Model):
....
def get_absolute_url(self):
return reverse('game:single_page', args=[str(self.id)])
And in your GameView:
class GameView(generic.TemplateView):
template_name = "matchview.html"
def get_context_data(self, **kwargs):
....
def get_success_url(self, **kwargs):
return self.object.get_absolute_url()

error: view didn't return HttpResponse object in generic class based views using mixin in Django?

I am writing a mixin that would enable for partial saving of form. For more details on this, please see my previous post.. while that is fixed, I have another error. I think the error comes from post method of mixing for not returnig HttpResponse Object. I am doing a super call which should take care of that. But that didn't work.
Error:
Exception Type: ValueError
Exception Value: The view blog.views.ArticleCreateView didn't return an HttpResponse object.
Here is the mixin:
class PendFormMixin(object):
form_hash_name = 'form_hash'
pend_button_name = 'pend'
def get_form_kwargs(self):
"""
Returns a dictionary of arguments to pass into the form instantiation.
If resuming a pended form, this will retrieve data from the database.
"""
form_hash = self.kwargs.get(self.form_hash_name)
print "form_hash", form_hash
if form_hash:
import_path = self.get_import_path(self.get_form_class())
return {'data': self.get_pended_data(import_path, form_hash)}
else:
print "called"
# print super(PendFormMixin, self).get_form_kwargs()
return super(PendFormMixin, self).get_form_kwargs()
def post(self, request, *args, **kwargs):
"""
Handles POST requests with form data. If the form was pended, it doesn't follow
the normal flow, but saves the values for later instead.
"""
self.object = None
if self.pend_button_name in self.request.POST:
print "here"
form_class = self.get_form_class()
print form_class
form = self.get_form(form_class)
# print "form is ", form
self.form_pended(form)
super(PendFormMixin, self).post(request, *args, **kwargs)
else:
super(PendFormMixin, self).post(request, *args, **kwargs)
# Custom methods follow
def get_import_path(self, form_class):
return '{0}.{1}'.format(form_class.__module__, form_class.__name__)
def get_form_hash(self, form):
content = ','.join('{0}:{1}'.format(n, form.data[n]) for n in form.fields.keys())
return md5(content).hexdigest()
def form_pended(self, form):
import_path = self.get_import_path(self.get_form_class())
form_hash = self.get_form_hash(form)
print "in form_pended"
pended_form = PendedForm.objects.get_or_create(form_class=import_path,
hash=form_hash)
print "form_class", import_path
print "form_hash", form_hash
print "pended_form", pended_form
for name in form.fields.keys():
pended_form[0].data.get_or_create(name=name, value=form.data[name])
print pended_form[0]
return form_hash
def get_pended_data(self, import_path, form_hash):
data = PendedValue.objects.filter(import_path=import_path, form_hash=form_hash)
return dict((d.name, d.value) for d in data)
Here is the View:
class ArticleCreateView(PendFormMixin, CreateView):
form_class = ArticleForm
model = Article
template_name = "article_create.html"
success_url = '/admin'
def form_valid(self, form):
"""
If the request is ajax, save the form and return a json response.
Otherwise return super as expected.
"""
if self.request.is_ajax():
self.object = form.save()
time.sleep(5)
return HttpResponse(json.dumps("success"),
mimetype="application/json")
return super(ArticleCreateView, self).form_valid(form)
def form_invalid(self, form):
"""
We haz errors in the form. If ajax, return them as json.
Otherwise, proceed as normal.
"""
if self.request.is_ajax():
return HttpResponseBadRequest(json.dumps(form.errors),
mimetype="application/json")
return super(ArticleCreateView, self).form_invalid(form)
The post method of a Class-Based View should return a HttpResponse object (as the exception says, obviously). In your mixin, you forgot to return something, hence the error. You just need to return the result from CreateView, from your super() call:
def post(self, request, *args, **kwargs):
"""
Handles POST requests with form data. If the form was pended, it doesn't follow
the normal flow, but saves the values for later instead.
"""
self.object = None
if self.pend_button_name in self.request.POST:
# You do things there
# It is the same line, you don't need an else block then. Note the "return"
return super(PendFormMixin, self).post(request, *args, **kwargs)
Calling super() does not make your method return anything, it only calls the superclass. You should use return super(...) in your PendFormMixin.post() method.

How can I get 'pk' or 'id' in `get_context_data` from CBV?

How can I get 'pk' or 'id' in get_context_data from CBV DetailView?
class MyDetail(DetailView):
model = Book
template_name = 'book.html'
def get_context_data(self, **kwargs):
context = super(MyDetail, self).get_context_data(**kwargs)
context['something'] = Book.objects.filter(pk=pk)
return context
url:
url(r'^book/(?P<pk>\d+)/$', MyDetail.as_view(), name='book'),
You can get it from self.kwargs['pk'].
I'm not sure why you want to, though, since the superclass already gets the Book corresponding to that pk - that's the whole point of a DetailView.
class MyDetail(DetailView):
model = Book
template_name = 'book.html'
def get_context_data(self, **kwargs):
context = super(MyDetail, self).get_context_data(**kwargs)
context['something'] =Book.objects.filter(pk=self.kwargs.get('pk'))
return context
self.kwargs['pk'] it doesn't work in Django 2.2
in DetailView
self.object is the object that this view is displaying.
So, to access the object's fields like id or pk just self.object.id or self.object.pk
So, The answer in Django 2.2 can be like:
class MyDetail(DetailView):
model = Book
template_name = 'book.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['something'] = Book.objects.filter(pk=self.object.pk) # <<<---
return context
Django 2.2 Doc
In get_context_data you already have the object in self.object (and you can do self.object.pk). Here's what happens upstream in the class hierarchy (DetailView inherits from BaseDetailView):
class BaseDetailView(SingleObjectMixin, View):
"""
A base view for displaying a single object
"""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
Reading Django source code to understand stuff is incredibly easy.
And by the way, I am not sure you can always rely on the fact that kwargs has a 'pk' key.
In addition to getting it from self.kwargs as Daniel Roseman suggested, you can use self.get_object().pk, for example if you change your URL identifier from pk to, say, slug or something.
you can simply get it in the 'get' method, like this:
def get_context_data(self, request, pk, *args, **kwargs):
context = super(MyDetail, self).get_context_data(**kwargs)
context['something'] =Book.objects.filter(pk=self.kwargs.get('pk'))
return context
def get_context_data(self, request, pk, *args, **kwargs):
context = super(MyDetail, self).get_context_data(**kwargs)
context['something'] =Book.objects.filter(pk=self.kwargs.get('pk'))
return context
Filter returns a query set that matches the lookup parameter (pk). Since 'pk' is unique, it would return the same results as get but for performance issues, ideally you'd want to use the get method to return one single object:
def get_context_data(self, request, pk, *args, **kwargs):
context = super(MyDetail, self).get_context_data(**kwargs)
context['something'] =Book.objects.get(pk=self.kwargs.get('pk'))
return context

How to pass ForeignKey value into initial data for Django form

I have a model like this:
class Job(models.Model):
slug = models.SlugField()
class Application(models.Model):
job = models.ForeignKey(Job)
And a view like this:
class ApplicationCreateView(CreateView):
model = Application
A user will view the job object (/jobs/<slug>/), then complete the application form for the job (/jobs/<slug>/apply/).
I'd like to pass application.job.slug as the initial value for the job field on the application form. I'd also like for the job object to be put in context for the ApplicationCreateView (to tell the user what job they're applying for).
How would I go about doing this in my view?
You may be interested in CreateView page of the fantastic http://ccbv.co.uk/ In this page, you can see in one glance which member methods and variables you can use.
In your case, you will be interested to override:
def get_initial(self):
# Call parent, add your slug, return data
initial_data = super(ApplicationCreateView, self).get_initial()
initial_data['slug'] = ... # Not sure about the syntax, print and test
return initial_data
def get_context_data(self, **kwargs):
# Call parent, add your job object to context, return context
context = super(ApplicationCreateView, self).get_context_data(**kwargs)
context['job'] = ...
return context
This has not been tested at all. You may need to play with it a little. Have fun.
I ended up doing the following in a function on my class:
class ApplicationCreateView(CreateView):
model = Application
form_class = ApplicationForm
success_url = 'submitted/'
def dispatch(self, *args, **kwargs):
self.job = get_object_or_404(Job, slug=kwargs['slug'])
return super(ApplicationCreateView, self).dispatch(*args, **kwargs)
def form_valid(self, form):
#Get associated job and save
self.object = form.save(commit=False)
self.object.job = self.job
self.object.save()
return HttpResponseRedirect(self.get_success_url())
def get_context_data(self, *args, **kwargs):
context_data = super(ApplicationCreateView, self).get_context_data(*args, **kwargs)
context_data.update({'job': self.job})
return context_data