What’s the difference between returning a `HttpResponseNotFound` and raising a `Http404` in Django?

An HttpResponseNotFound is just like a normal HttpResponse except it sends error code 404. So it’s up to you to render an appropriate 404 page in that view, otherwise the browser will display its own default. Raising an Http404 exception will trigger Django to call its own 404 error view. Actually, this does little more … Read more

Django search fields in multiple models

One solution is to query all the models # Look up Q objects for combining different fields in a single query from django.db.models import Q people = Person.objects.filter(Q(first_name__contains=query) | Q(last_name__contains=query) restaurants = Restaurant.objects.filter(restaurant_name__contains=query) pizzas = Pizza.objects.filter(pizza_name__contains=query) Then combine the results, if you want from itertools import chain results = chain(people, restaurants, pizzas) Ok, sure, here’s … Read more

Can I redirect to another url in a django TemplateView?

I know this question is old, but I’ve just done this myself. A reason you may think you want to do it in get_context_data is due to business logic, but you should place it in dispatch. def dispatch(self, request, *args, **kwargs): if not request.user.is_authenticated: return redirect(‘home’) return super().dispatch(request, *args, **kwargs) Keep your business logic in … Read more

django – what goes into the form action parameter when view requires a parameter?

If you want to explicitly set the action, assuming you have a variable username in your template, <form name=”form” method=”post” action=”{% url myview.views username %}”> or you could assign a name for the url in your urls.py so you could reference it like this: # urls.py urlpatterns += patterns(‘myview.views’, url(r’^(?P<user>\w+)/’, ‘myview’, name=”myurl”), # I can’t … Read more

Django – two views, one page

A view in django is just any callable that ultimately returns a Response object. Within that view, you could split the work up into whatever organization suits you. Maybe your view 100% delegates out to other methods. In your case, your main view would call 2 other functions for data. These could also be views … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)