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
3 answers

Applying bootsrap and custom css to {{form}} elements in django

I am trying to apply CSS to individual {{form}} fields on my html template. I am referencing this solved question: CSS styling in Django forms` using the answer I created my forms.py page as follows: from django import forms from .models import…
Darryl Lobo
  • 155
  • 3
  • 18
3
votes
1 answer

RelatedObjectDoesNotExist Exception

I tried to define prefix of signing in user, but I got this exception RelatedObjectDoesNotExist. The thing is django cannot find user Student and Parent(Exception Value: User has no student) I want to render different templates according to user's…
jixad
  • 65
  • 1
  • 6
3
votes
1 answer

Multiple images in django form with multiupload

I need to add multiple images in django form to one model. I did a research and for form outside of django I try to setup django-multiupload. My models.py: class Profile(models.Model): ... ... first = models.ImageField("first",…
3
votes
2 answers

Django: DetailView get objects from foreignkey

I have a class-based DetailView for my model Event and want to show kategorie entries that are related by foreignkey. models.py class Event(models.Model): name = models.CharField(max_length=50) def get_absolute_url(self): return…
user7084802
3
votes
2 answers

MultipleObjectsReturned - get() returned more than one ContentType -- it returned 2

everything was working fine then suddenly i got this error MultipleObjectsReturned at /rohit/post/new-post/ get() returned more than one ContentType -- it returned 2! i do not know why it returned 2 objects. it just suppose to return one and i…
Rohit Chopra
  • 567
  • 1
  • 8
  • 24
3
votes
4 answers

Production django server throwing "NoReverseMatch" while rendering, works on development

Caught NoReverseMatch while rendering: Reverse for 'views.main' with arguments '()' and keyword arguments '{}' not found. I don't understand what would cause the error. My urls urlpatterns = patterns('', url(r'^$', views.main), html template
bdd
  • 3,436
  • 5
  • 31
  • 43
3
votes
5 answers

Django: How to Convert QuerySet into String

I'm using Python 3 and trying to convert a QuerySet into human-readable text. I have a line like this: top_post = Post.objects.filter(category='1')[:1] That prints like this: ]> What makes me scratch my head is a…
E. Sutherland
  • 101
  • 2
  • 3
  • 9
3
votes
1 answer

Save model with @transaction.atomic in class based view

I am trying to handle both add new and edit object. My views.py file is like- personal_data=Personal.objects.create(emp_fname=first_name, emp_mname=middle_name, emp_lname=last_name) # rest of object is created here try: print "pk", pk with…
V.Khakhil
  • 285
  • 5
  • 22
3
votes
1 answer

Django render not working when redirected from another view

I have two views as follows def home(request): levels = levelData.objects.all() context ={ "levels" : levels } print "abcde" return render(request, 'home.html', context) and the other one def create(request): if…
3
votes
2 answers

Update to 1.11: TypeError build_attrs() takes at most 2 arguments (3 given)

I a updating from 1.10.7 to 1.11.0 and I am getting the following error when viewing a form. I cannot fathom what is wrong with my form at all. Other forms work in the same way. I suspect that it could be an interaction with either select2 or…
3
votes
1 answer

Django Class BasedView - UpdateView with multiple models and multiple forms

I have a list of users or users/roles type. I want to be able to click a link to Update their respective profiles users. When I perform click in Profile user, I should be able to edit the associated data to respective user according to profile that…
bgarcial
  • 2,915
  • 10
  • 56
  • 123
3
votes
2 answers

Django REST overrride destroy method to make user inactive

I'm trying to first access the users table via the user foreign key present in userinformations models and later override the RetriveUpdateDestroy API view's destroy method to change the status of the user to inactive instead of deleting them. I…
3
votes
1 answer

django remove source files and generate pyc files

I want to remove all .py files in my django project.But pyc files are not generated as yet.. What is the settings that needs to be changed to generate the .pyc files
Rajeev
  • 44,985
  • 76
  • 186
  • 285
3
votes
3 answers

Get the first element in the for loops in the Django template

Template: {% for code in group_codes %} *_{{ code.build }}_*
{% if test_info.test_type = 0 %} {{ code.pre_testing_fail }}/{{ code.pre_testing_total }} failed pre-test
{% else %} {% for…
pylearner
  • 537
  • 2
  • 8
  • 26
3
votes
1 answer

Cannot pass Helper to django Crispy Formset in template

I'm trying to set up crispy form on a form and a formset (Visit and VisitService). I'm having trouble attaching the helper to the formset, no matter how I do it. I added helper as an attribute to the Formset and added the helper to view and context…