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
3
votes
1 answer

Django custom button counter

I want to have 2 buttons at the bottom of my each post like that: I need suggestions how could I achieve that. I tried django-vote library, but it does not work for me. My code: <--template.html-->
3
votes
1 answer

How can I sort my model output in Django?

I'm quite new in the development with django and I hope someone can help me. I have two main models in one view and in one template. Each one of my "main" models has its own "sub" model (with a foreign key). My output is fine and it works so far. My…
3
votes
1 answer

Login required always render to home page

when I am using login_required it does not rendering to appropriate url it always render to home page only login view def login_view(request): print(request.user.is_authenticated()) w="Welcome" title = "Login" form = UserLoginForm(request.POST or…
Mandeep Thakur
  • 655
  • 1
  • 10
  • 23
3
votes
2 answers

Django how to reuse views functionality common to all views

I have a Django template, let's call it index.html which is divided in 3 parts (header, content and footer). In the header section I have a search bar that includes a drop-down menu which allows the user to select an option from it and search things…
3
votes
1 answer

render two views to a single html template

I have two views and I want to render those views to a single HTML page. I know how to render a single view to an HTML page but don't know how to render two views to a single HTML page. views.py file from django.shortcuts import render from…
Mandeep Thakur
  • 655
  • 1
  • 10
  • 23
3
votes
1 answer

Django - How to redirect differently using LoginRequired and PermissionRequired?

I would like to create a Mixin which will: First - Check if a user is authenticated, if not, redirect to login url. If yes... Second - Check if user has a defined Profile (a user), if not, redirect to Profile creation, else, allow user to access the…
pedrovgp
  • 767
  • 9
  • 23
3
votes
2 answers

How to add extra fields using django.forms Textarea

I am newbie in django and working on a pootle project. I would like to add bio (in textarea), interests (textarea), and profile pics (image upload). this page is looking like this: http://pootle.locamotion.org/accounts/personal/edit/ (you might…
3
votes
1 answer

Django TypeError at / render() got an unexpected keyword argument 'context_instance'

Currently setting up a Django web application on Azure, deployed through Git locally. I haven't actually written any code yet, and when I start up the development server using python3 manage.py runserver and go to the site's address I get the…
green.mango
  • 201
  • 1
  • 4
  • 9
3
votes
2 answers

Django - Pass related object as variable in user_passes_test mixin

I am trying to restrict instances of an object to be viewable only to users referenced by that object via a OneToOneField. I’m using the “user_passes_test” mixin on a DetailView to compare request.user to the user in the OnetoOne relationship. I got…
brandondavid
  • 201
  • 1
  • 8
3
votes
2 answers

Django Edit Form Data: data is duplicated instead of being updated

How can I update an existing record rather then add a new one which is my problem. Right now I'm trying to edit existing product data in the edit form and save the new changes. But instead of existing product data being updated, I get a new product,…
Sophie
  • 31
  • 1
  • 5
3
votes
2 answers

how to use django rest filtering with mongoengine for list filtering

views.py from __future__ import unicode_literals from rest_framework_mongoengine.viewsets import ModelViewSet as MongoModelViewSet from app.serializers import * from rest_framework_mongoengine.generics import * from rest_framework import…
3
votes
1 answer

Getting a "bound method" error with my login form

So my register form works fine, it successfully creates a new user. However my login form cannot log a user in. So when I log in with username/password entered correctly, and print(form.non_field_errors) in my view, I get this error:
Zorgan
  • 8,227
  • 23
  • 106
  • 207
3
votes
2 answers

request object does not have a user Django

I am making a wallet app so if someone goes to http://127.0.0.1:8000/add_money/ to add money and they press submit the money has to be added to their wallet but after submit there is an error: AttributeError at /add_money/ 'unicode' object has no…
Ravin Kohli
  • 143
  • 10
3
votes
1 answer

Django: Passing object from template to views

I want to use the same template to view information about each of my database objects. I would like to be able to click on each element in the list and have it link me to a page with info about it. I'm thinking there's an easier way than making a…
tchane
  • 33
  • 1
  • 1
  • 3
3
votes
0 answers

Django auto complete light outside admin - not working

I trying to set up the django auto complete light outside the admin views. I am follow this tutorial but I havent got any luck in figure out whats is…