Questions tagged [django-views]

Django views are MVC views; they control rendering (typically through templates), and the data displayed.

Django views are MVC views; they control rendering (typically through templates) and the data displayed.

It is possible to create generic views, which are specialised on various parameters (frequently model classes), which are simply paired with an appropriate template to create a complete page.

The separation from the template system also makes it very easy to create output in different formats, or use one view for several, quite different looking pages (usually with similar data).

There are two types of views: the Class based view (CBV for short) and Function based views (FBV). The use cases differ for each one and whilst later versions of Django advocate the use of Class based views the Function based views aren't fully deprecated.

CBV's enables better code reuse, inheritance and mixins. Further reading can be found here

An example of a Class based view:

from django.views.generic import UpdateView
from myapp.models import Author

class AuthorUpdate(UpdateView):
    model = Author
    fields = ['name']
    template_name_suffix = '_update_form'

An example of a Function based view:

def update_account_view(request, account_id):
    # Do some account stuff
    context = {'account': some_object, 'another_key': 'value'}
    return render_to_response('templates/account_update.html', 
                              context, 
                              context_instance=RequestContext(request))
24045 questions
86
votes
16 answers

ValueError: Missing staticfiles manifest entry for 'favicon.ico'

I'm getting a ValueError when running python manage.py test. My project is named fellow_go, and I'm currently working on an App called pickup. Please note that this error is added in a relatively recent commit to Django: Fixed #24452 -- Fixed…
nalzok
  • 14,965
  • 21
  • 72
  • 139
85
votes
10 answers

How to get the username of the logged-in user in Django?

How can I get information about the logged-in user in a Django application? For example: I need to know the username of the logged-in user to say who posted a Review:

Publica tu tuit, {{…

Cris Towi
  • 1,141
  • 2
  • 10
  • 15
81
votes
3 answers

How to receive json data using HTTP POST request in Django 1.6?

I am learning Django 1.6. I want to post some JSON using HTTP POST request and I am using Django for this task for learning. I tried to use request.POST['data'], request.raw_post_data, request.body but none are working for me. my views.py is …
Alok
  • 7,734
  • 8
  • 55
  • 100
78
votes
7 answers

Raw SQL queries in Django views

How would I perform the following using raw SQL in views.py? from app.models import Picture def results(request): all = Picture.objects.all() yes = Picture.objects.filter(vote='yes').count() return render_to_response( …
David542
  • 104,438
  • 178
  • 489
  • 842
78
votes
2 answers

Django test RequestFactory vs Client

I am trying to decide whether I should use Django's Client or RequestFactory to test my views. I am creating my server using DjangoRESTFramework and it's really simple, so far: class SimpleModelList(generics.ListCreateAPIView): """ Retrieve…
78
votes
2 answers

Django check if object in ManyToMany field

I have quite a simple problem to solve. I have Partner model which has >= 0 Users associated with it: class Partner(models.Model): name = models.CharField(db_index=True, max_length=255) slug = models.SlugField(db_index=True) user =…
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
76
votes
4 answers

Redirect / return to same (previous) page in Django?

What are the options when you want to return the user to the same page in Django and what are the pros/cons of each? Methods I know: HTTP_REFERER GET parameter containing the previous URL Session data to store the previous URL Are there any other?
Al Bundy
  • 1,599
  • 1
  • 14
  • 20
75
votes
7 answers

matching query does not exist Error in Django

I have implemented a password recovery functionality in django. With my method, the new password will be sent to the email id entered. It works fine when given the correct email (e-mail id which exists in the database). But when given an email id…
rv_k
  • 2,383
  • 7
  • 39
  • 52
75
votes
4 answers

Django request to find previous referrer

I'm passing the request to the template page.In django template how to pass the last page from which the new page was initialised.Instead of history.go(-1) i need to use this {{request.http referer}} ??
Rajeev
  • 44,985
  • 76
  • 186
  • 285
75
votes
16 answers

Django - is not a registered namespace

I am trying to process a form in django/python using the following code. home.html: views.py: def submit(request): a = request.POST(['initial']) return render(request,…
Programmerr
  • 961
  • 3
  • 9
  • 11
73
votes
10 answers

How to specify the login_required redirect url in django?

I have a view function: @login_required def myview(): # do something # respond something pass How can I specify the exact URL for this view function to be redirected?
Pol
  • 24,517
  • 28
  • 74
  • 95
72
votes
11 answers

How to create password input field in django

Hi I am using the django model class with some field and a password field. Instead of displaying regular plain text I want to display password input. I created a model class like this: class UserForm(ModelForm): class Meta: password =…
Dar Hamid
  • 1,949
  • 4
  • 21
  • 27
72
votes
11 answers

How to expire Django session in 5minutes?

I'm using this to login the user in: def login_backend(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username,…
pynovice
  • 7,424
  • 25
  • 69
  • 109
71
votes
4 answers

base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute

I'm trying to replace the standard queryset: queryset: MyModel.objects.all() on my: def get_queryset(self, username=None): if username is not None: user = UserModel.objects.get(username=username) queryset =…
woe-dev.
  • 933
  • 1
  • 7
  • 12
70
votes
8 answers

Django - after login, redirect user to his custom page --> mysite.com/username

By default after login django redirects the user to an accounts/profile page or if you edit the LOGIN_REDIRECT_URL you can send the user to another page you specify in the settings.py. This is great but I would like the user (after login) to be…
avatar
  • 12,087
  • 17
  • 66
  • 82