I am learning to use Ajax with Django, many tutorials simply check if request.method == 'GET' or POST. I am curious for what do we need .is_ajax() then. Is it normal no to use it or tutorials just show basic concepts?
I am curious for what do we need .is_ajax() then. Is it normal no to
use it or tutorials just show basic concepts?
Yes, it is totally normal not to use is_ajax. Most of the time what you care about in your views is the HTTP verb (e.g. GET, POST, PATCH..).
However there are certain cases where you want to know if the request is an AJAX request. Why? because you might want to return a different result depending if the request is ajax or not.
The most common use for this solution is PJAX. When you use a pjax technology, if the request is not an ajax request you render the entire page, whereas if the request comes from ajax you render only a partial of the page. Then the partial page is added in the correct place in the webpage by some sort of lib, such as https://github.com/defunkt/jquery-pjax.
For example, this is a mixing I wrote to use Pjax in django:
import os
from django.views.generic.base import TemplateResponseMixin
class PJAXResponseMixin(TemplateResponseMixin):
pjax_template_name = None
pjax_suffix = "pjax"
pjax_url = True
def get_context_data(self, **kwargs):
context = super(TemplateResponseMixin, self).get_context_data(**kwargs)
context['inner_template'] = self.pjax_template_name
return context
def get_template_names(self):
names = super(PJAXResponseMixin, self).get_template_names()
if self.request.is_ajax():
if self.pjax_template_name:
names = [self.pjax_template_name]
else:
names = self._pjaxify_template_var(names)
return names
def get(self, request, *args, **kwargs):
response = super(PJAXResponseMixin, self).get(request, *args, **kwargs)
if sel
f.pjax_url :
response['X-PJAX-URL'] = self.request.path
return response
def _pjaxify_template_var(self, template_var):
if isinstance(template_var, (list, tuple)):
template_var = type(template_var)(self._pjaxify_template_name(name) for name in template_var)
elif isinstance(template_var, basestring):
template_var = self._pjaxify_template_name(template_var)
return template_var
def _pjaxify_template_name(self, name):
container = self.request.META.get('HTTP_X_PJAX_CONTAINER', False)
if container is not False:
name = _add_suffix(name, clean_container_name(container))
return _add_suffix(name, self.pjax_suffix)
#################################################
# HELPER METHODS #
#################################################
def clean_container_name(name):
return name.replace('#', '')
def _add_suffix(name, suffix):
if "." in name:
file_name, file_extension = os.path.splitext(name)
name = "{0}-{1}{2}".format(file_name, suffix, file_extension)
else:
name += "-{0}".fomat(suffix)
return name
Basically, this mixing renders the default template if the request is not an ajax request. Whereas if the request is AJAX, it renders the pjax_template, if there is one, or the name of the default template prefixed with pjax.
Related
I am trying to use reverse method in django view but I got an exception 'str' object has no attribute 'get'.
here is my view
class AbandonTicketView(View):
context = dict()
template_name = "places/order_detail.html"
def get(self, request, uidb64, token, ordercode):
order = abandon_oder(uidb64, token, ordercode)
if order is not None and order.paid is False:
return reverse("order_detail", kwargs={"ordercode": order.code})
return redirect("tickets")
view that I want to go:
class OrderDetailView(LoginRequiredMixin, View):
template_name = "places/order_detail.html"
context = dict()
def get(self, request, ordercode):
order = Order.objects.get(code=ordercode)
self.context["order"] = order
self.context["key"] = settings.tycoon.STRIPE_PUBLISHABLE_KEY
if order.send_count >= 3 and order.paid is False:
self.context["abandon_ticket"] = "Order was canceled"
return render(request, template_name=self.template_name, context=self.context)
def post(self, request, ordercode):
order = pay_for_ticket(ordercode, request.POST["stripeToken"], request.user)
self.context["order"] = order
return render(request, template_name=self.template_name, context=self.context)
here is url:
path("orders/<code:ordercode>/detail/", views.OrderDetailView.as_view(), name="order_detail"),
path("tickets/", views.OrderedTicketsView.as_view(), name="tickets"),
I don't really know why it happends, because I do the similar reverse earlier and everything works fine, but not now. Could you help me please to solve this problem?
reverse() returns a string, but your view has to return a HttpResponse.
Change your line:
return reverse("order_detail", kwargs={"ordercode": order.code})
to also use redirect() (like the other part of your view)
return redirect("order_detail", args=[order.code, ])
or maybe even simplified like this
return redirect("order_detail", order.code)
Does that work?
You could use redirect with reverse and it works.
from django.shortcuts import redirect
return redirect(reverse("order_detail", kwargs={"ordercode": order.code}))
OR the second keyword to do the same thing is:
from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse("order_detail", kwargs={"ordercode": order.code}))
The reasoning behind this is that reverse just returns the URL in the form of a string. When you want to redirect an HTTP Response should happen. Only cases where only the URL is required reverse should be used otherwise you need to generate an HTTP Response as well.
I created the FormView below that will dynamically return a form class based on what step in the process that the user is in. I'm having trouble with the get_form method. It returns the correct form class in a get request, but the post request isn't working.
tournament_form_dict = {
'1':TournamentCreationForm,
'2':TournamentDateForm,
'3':TournamentTimeForm,
'4':TournamentLocationForm,
'5':TournamentRestrictionForm,
'6':TournamentSectionForm,
'7':TournamentSectionRestrictionForm,
'8':TournamentSectionRoundForm,}
class CreateTournament(FormView):
template_name = 'events/create_tournament_step.html'
def __init__(self, *args, **kwargs):
form_class = self.get_form()
success_url = self.get_success_url()
super(CreateTournament, self).__init__(*args, **kwargs)
def get_form(self, **kwargs):
if 'step' not in kwargs:
step = '1'
else:
step = kwargs['step']
return tournament_form_dict[step]
def get_success_url(self, **kwargs):
if 'step' not in kwargs:
step = 1
else:
step = int(kwargs['step'])
step += 1
if 'record_id' not in kwargs:
record_id = 0
else:
record_id = int(kwargs['record_id'])
return 'events/tournaments/create/%d/%d/' % (record_id, step)
The post request fails at the django\views\generic\edit.py at the get_form line, which I realize is because I've overwritten it in my FormView:
def post(self, request, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
form = self.get_form()
if form.is_valid(): …
return self.form_valid(form)
else:
return self.form_invalid(form)
However, when I change the name of my custom get_form method to say gen_form, like so:
def __init__(self, *args, **kwargs):
form_class = self.gen_form()
success_url = self.get_success_url()
super(CreateTournament, self).__init__(*args, **kwargs)
def gen_form(self, **kwargs):
if 'step' not in kwargs:
step = '1'
else:
step = kwargs['step']
return tournament_form_dict[step]
my form class doesn't get processed in the get request and evaluates to None. I'm scratching my head as to why when I override the get_form method, it works, but my own named method doesn't? Does anyone know what the flaw might be?
Django's FormMixin [Django-doc] defines a get_form function [Django-doc]. You here thus basically subclassed the FormView and "patched" the get_form method.
Your attempt with the gen_form does not work, since you only defined local variables, and thus do not make much difference anyway, only the super(..) call will have some side effects. The other commands will keep the CPU busy for some time, but at the end, will only assign a reference to a Form calls to the form_class variable, but since it is local, you will throw it away.
That being said, your function contains some errors. For example the **kwargs will usually contain at most one parameter: form_class. So the steps will not do much. You can access the URL parameters through self.args and self.kwargs, and the querystring parameters through self.request.GET. Furthermore you probably want to patch the get_form_class function anyway, since you return a reference to a class, not, as far as I understand it, a reference to an initilized form.
Constructing URLs through string processing is probably not a good idea either, since if you would (slightly) change the URL pattern, then it is likely you will forget to replace the success_url, and hence you will refer to a path that no longer exists. Using the reverse function is a safer way, since you pass the name of the view, and parameters, and then this function will "calculate" the correct URL. This is basically the mechanism behind the {% url ... %} template tag in Django templates.
A better approach is thus:
from django.urls import reverse
class CreateTournament(FormView):
template_name = 'events/create_tournament_step.html'
def get_form_class(self):
return tournament_form_dict[self.kwargs.get('step', '1')]
def get_success_url(self):
new_step = int(self.kwargs.get('step', 1)) + 1
# use a reverse
return reverse('name_of_view', kwargs={'step': new_step})
I been working with Django Forms for a while but recently I had to create a form to search for data with a MultipleChoiceField.
Since the URL must be shared between the users the form performs a GET to the server to keep the search parameters in the query-string.
The problem is that if multiple options are checked the length of the URL increases too much. For example:
http://www.mywebsite.com/search?source=1&source=2&source=3...
Is there anyway working with django forms to get a url like the following:
http://www.mywebsite.com/search?source=1-2-3...
Or is it a better approach to create a token that compress the query-string parameters?
The form is then used to make a search over ElasticSearch. I'm not using djangos models.
Thanks!
Overriding get and get_context_data on a TemplateView could work. Then you could have a URL like this: http://www.mywebsite.com/search?sources=1,2
class ItemListView(TemplateView):
template_name = 'search.html'
def get(self, request, *args, **kwargs):
sources = self.request.GET.get('sources')
self.sources = sources.split(',') if sources else None
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.sources:
context['search-results'] = self.get_search_results(
self.sources,
)
return context
def get_search_results(self, sources):
"""
Retrieve items that have `sources`.
"""
# ElasticSearch code here…
data = {
'1': 'Honen',
'2': 'Oreth',
'3': 'Vosty',
}
return [data[source_id] for source_id in sources]
Now, if the /search?sources=1,2 URL was requested the template context would have Honen and Oreth in it as the variable search-results.
I've been experimenting with Django's Class Based Views and am trying to write a simple class based view that processes certain information in request so that the processed information can be used by the "handler" method.
I don't seem to have fully understood what the docs say and am unsure of whether this should be a Mixin, a generic view or something else. I'm thinking of making a class like this:
class MyNewGenericView(View):
redirect_on_error = 'home'
error_message = 'There was an error doing XYZ'
def dispatch(self, request, *args, **kwargs):
try:
self.process_information(request)
# self.process_information2(request)
# self.process_information3(request)
# etc...
except ValueError:
messages.error(request, self.error_message)
return redirect(self.redirect_on_error)
return super(MyNewGenericView, self).dispatch(request, *args, **kwargs)
def process_information(self, request):
# Use get/post information and process it using
# different models, APIs, etc.
self.useful_information1 = 'abc'
self.useful_information2 = 'xyz'
def get_extra_info(self):
# Get some extra information on something
return {'foo':'bar'}
This will allow someone to write a view like:
class MyViewDoesRealWork(MyNewGenericView):
def get(self, request, some_info):
return render(request, 'some_template.html',
{'info':self.useful_information1})
def post(self, request, some_info):
# Store some information, maybe using get_extra_info
return render(request, 'some_template.html',
{'info':self.useful_information1})
Is the above code the right way to go? Is there any simpler/better way of doing this? Will this prevent the above functionalities from being used in another generic view (e.g. a built-in generic view)?
Have a look at this. great example code. http://www.stereoplex.com/blog/get-and-post-handling-in-django-views
It seems I just asked a stupid question.
This can easily be achieved by making a class that processes that information:
class ProcessFooInformation(object):
def __init__(self, request):
self.request = request
#property
def bar(self):
baz = self.request.GET.get('baz', '')
# do something cool to baz and store it in foobar
return foobar
# etc...
Then using old style function views or new class-based views:
def my_view(request):
foo = ProcessFooInformation(request)
# use foo in whatever way and return a response
return render(request, 'foobar.html', {'foo':foo})
I also made this more efficient by using lazy evaluation of properties.
I adapted ideas from the lazy property evaluation recipe and the comments to write a wrapper:
def lazy_prop(func):
def wrap(self, *args, **kwargs):
if not func.__name__ in self.__dict__:
self.__dict__[func.__name__] = func(self, *args, **kwargs)
return self.__dict__[func.__name__]
return property(wrap)
This evaluates the value of the wrapped method only once per instance and uses a stored value on subsequent calls. This is useful if the property evaluates slowly.
I've been pulling from various questions on StackOverflow to try to figure out how to work with ModelMultipleChoiceFields within a form. I almost have a working form that allows users to select languages to translate an article to. I created a form that takes a SourceArticle as the first constructor argument and uses it to specify the queryset for the languages field of my form.
class AddTargetLanguagesForm(forms.Form):
def __init__(self, article=None, *args, **kwargs):
super(AddTargetLanguagesForm, self).__init__(*args, **kwargs)
self.fields['languages'].queryset = Language.objects.exclude(
Q(id = article.language.id) |
Q(id__in=[o.id for o in article.get_target_languages()]) |
Q(code="templates"))
languages = forms.ModelMultipleChoiceField(_("Languages"))
Note that my AddTargetLanguagesForm is not based on a ModelForm, because it is not directly related to any of my model objects.
When I render the form for the first time, it correctly provides me with languages that (a) aren't the source language, (b) aren't already selected, and (c) aren't the special "templates" language. However, when I try to post my form, I get the following error:
AttributeError: 'QueryDict' object has
no attribute 'language'
I assume that this is related to how forms work in Django, but I'm pretty new. Rather than accepting a SourceArticle as the first parameter in my constructor, a QueryDict is placed instead. I assume that this contains the POST params from the request. How do I need to modify my code to allow it to capture the selected languages?
Here is a copy of my view, if it helps you see how I'm using the form.
#login_required
def add_target_languages(request, aid, template_name="wt_articles/add_target_languages.html"):
"""
Adds one or more target language translations to a source article.
"""
content_dict = {}
# Fetch the article
no_match = False
sa_set = SourceArticle.objects.filter(id=aid)
if len(sa_set) < 1:
no_match = True
content_dict['no_match'] = no_match
else:
article = sa_set[0]
content_dict['article'] = article
if request.method == "POST":
target_language_form = AddTargetLanguagesForm(request.POST)
if target_language_form.is_valid():
languages = target_language_form.cleaned_data['languages']
article.add_target_languages(languages)
return HttpResponseRedirect('/articles/list')
else:
target_language_form = AddTargetLanguagesForm(article)
content_dict['target_language_form'] = target_language_form
return render_to_response(template_name, content_dict,
context_instance=RequestContext(request))
This line is your problem:
target_language_form = AddTargetLanguagesForm(request.POST)
That's the standard way of instantiating a form from a POST, but the trouble is that you've rewritten the method signature of AddTargetLanguagesForm.__init__:
def __init__(self, article=None, *args, **kwargs):
so that the first positional argument (after the automatic self), is article. You could change the instantiation, but I prefer to do this:
def __init__(self, *args, **kwargs):
article = kwargs.pop('article', None)
super(AddTargetLanguagesForm, self).__init__(*args, **kwargs)
if article is not None:
...etc...