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
56
votes
4 answers

add request.GET variable using django.shortcuts.redirect

Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) If I do redirect('url-name', x) I get HttpResponseRedirect('/my_long_url/%s/', x) I don't have complains using…
juanefren
  • 2,818
  • 6
  • 32
  • 41
55
votes
1 answer

How do I use CreateView with a ModelForm

I get an error in my class AuthorCreateForm when I submit my form. NameError self is not defined How do I use a CreateForm? I have created a class in my Author.py file from django.views.generic import TemplateView, ListView, CreateView from…
iJK
  • 4,655
  • 12
  • 63
  • 95
54
votes
1 answer

django: Purpose of django.utils.functional.SimpleLazyObject?

I ran into an issue where I assigned request.user to a variable called prior_user, then essentially authenticated the user, then checked to see if request.user != prior_user. I expected them not to be the same and that prior_user should contain…
donogood
  • 555
  • 1
  • 4
  • 7
53
votes
3 answers

How do I call a Django function on button click?

I am trying to write a Django application and I am stuck at how I can call a view function when a button is clicked. In my template, I have a link button as below, when clicked it takes you to a different webpage:
Dev
  • 1,529
  • 4
  • 24
  • 36
52
votes
4 answers

How do I invalidate @cached_property in django

I am currently using @cached_property on a model class and I would like to delete it on save so that it can be repopulated on the next call. How do I do this? Example: class Amodel(): #...model_fields.... @cached_property def…
user1711168
  • 677
  • 1
  • 6
  • 12
52
votes
1 answer

Can I have a Django form without Model

Can I have a Form in my template which is not backed by a model. I do not need to store the data just need that data to generate a POST request of my own in the view. Template - The form with text fields. View - get data from form, and generate…
Adroit
  • 701
  • 1
  • 6
  • 18
52
votes
2 answers

Override a form in Django admin

In Django admin I want to override and implement my own form for a model (e.g. Invoice model). I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a…
18bytes
  • 5,951
  • 7
  • 42
  • 69
51
votes
5 answers

Django serve static index.html with view at '/' url

I have my index.html in /static/ folder. My django app is running ok when i try: http://127.0.0.1:8000/index.html But i want to acces index.html by url: http://127.0.0.1:8000/ I wrote a view and it works: class IndexView(TemplateView): …
Feanor
  • 3,568
  • 5
  • 29
  • 49
49
votes
1 answer

How do you use get_context_data with TemplateView in Django

I'm trying to do something like this: class AboutView(TemplateView): template_name = 'about.html' def get_context_data(self, **kwargs): context = super(AboutView, self).get_context_data(**kwargs) context['dahl_books'] =…
9-bits
  • 10,395
  • 21
  • 61
  • 83
48
votes
4 answers

Django how to partial render

How do I call a view method from a template level like partial render in RoR? The problem is perfectly illustrated in this blog. I can use include to include templates in templates but then I would have to match all the variable names across layers…
xster
  • 6,269
  • 9
  • 55
  • 60
48
votes
4 answers

Difference between APIView class and viewsets class?

What are the differences between the APIView and ViewSets classes? I am following Django REST-framework official documentation. I find it lacking in examples. Can you explain the above difference with a suitable example?
user7139313
47
votes
1 answer

Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?

I'm using Django 1.3 and I realize it has a collectstatic command to collect static files into STATIC_ROOT. Here I have some other global files that need to be served using STATICFILES_DIR. Can I make them use the same dir ? Thanks.
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
46
votes
5 answers

get class name for empty queryset in django

I have empty queryset of model Student students = Students.objects.all() If the above queryset is empty, then how can i get the model(class name)? How can i get the model name for empty queryset? EDIT: How can i get the app name from the queryset?
Asif
  • 1,775
  • 4
  • 25
  • 39
45
votes
5 answers

Add data to ModelForm object before saving

Say I have a form that looks like this: forms.py class CreateASomethingForm(ModelForm): class Meta: model = Something fields = ['field2', 'field3', 'field4'] I want the form to have these three fields. However my Somethingclass…
Joker
  • 2,119
  • 4
  • 27
  • 38
44
votes
9 answers

How to change the file name of an uploaded file in Django?

Is it possible to change the file name of an uploaded file in django? I searched, but couldn't find any answer. My requirement is whenever a file is uploaded its file name should be changed in the following format. format = userid + transaction_uuid…
Software Enthusiastic
  • 25,147
  • 16
  • 58
  • 68