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: sum values in for loop

i want to sum of the orders price in formset. how to sum int(food.price) * int(f.order_count) ? def reservation_f(request, year, month, day): # order_count = Food.objects.filter(serve_date__startswith = datetime.date(int(year), int(month),…
shahin
  • 159
  • 1
  • 7
3
votes
1 answer

How to get parameters from current url

Is it possible to get an specific parameter in a url and use it in a template ? `{{ request.path }} It gets the whole url, i need only the 'pk' parameter, to make a link to another page. Thanks.
Goun2
  • 417
  • 1
  • 11
  • 22
3
votes
2 answers

django query eliminate duplicates

In the following query how to eliminate the duplicates, d_query = Profile.objects.filter(company="12") search_string ="Tom" if search_string != "": d_query = d_query.filter(Q(profiles__name__icontains=search_string) | …
Hulk
  • 32,860
  • 62
  • 144
  • 215
3
votes
2 answers

ValueError: not enough values to unpack (expected 2, got 1)

the following is my code views.py from django.shortcuts import render from .forms import MedicineForm from .models import Medicine def index(request): all_medicine = Medicine.objects.order_by('id') return render(request,…
dogewang
  • 648
  • 1
  • 7
  • 15
3
votes
1 answer

How to save inline formset user field in Django using views

I've been using this great post http://kevindias.com/writing/django-class-based-views-multiple-inline-formsets/ to setup my site. I was wondering how to save the user field automatically to an inline formset in views (I used the blockquote for…
mloch
  • 89
  • 1
  • 5
3
votes
2 answers

how to uploads images on a web server. django

I want to display images on my web site and I can't find my mistake. In other posts I saw that need to upgrade settings.py, but I set MEDIA_ROOT and MEDIA_URL as in example but it doesn't help me. models: class News(models.Model): title =…
3
votes
3 answers

How to change to string and remove ' from Django tempalates / context?

I currently have a date that is formatted in unicode: k = u'2015-02-01' I tried to add this to a list and change it into a string: date = [] date.append(str(k)) Then I want to pass this as a Django context to my template. However, the date is…
H C
  • 1,138
  • 4
  • 21
  • 39
3
votes
0 answers

How to accommodate APIView & ViewSet views in urls.py

How would one write a urls.py file to accommodate views created from APIView and ViewSet. entity.views.py from .models import Entity from .serializers import EntitySerializer class EntityViewSet(DefaultsMixin, ListCreateRetrieveUpdateViewSet): …
lukik
  • 3,919
  • 6
  • 46
  • 89
3
votes
1 answer

Django django.test Client post request

Working on hellowebapp.com Please help test valid form post using Django test client post request? response = self.client.post('/accounts/create_thing/', { 'name': dummy_thing.name, 'description': dummy_thing.description, }) Here's the TestCase…
Impavid
  • 435
  • 1
  • 6
  • 17
3
votes
1 answer

How to read the json file of a dynamical way in relation to their size structure

I have the following JSON file named ProcessedMetrics.json which is necessary read for send their values to some template: { "paciente": { "id": 1234, "nombre": "Pablo Andrés Agudelo Marenco", "sesion": { "id": 12345, …
bgarcial
  • 2,915
  • 10
  • 56
  • 123
3
votes
2 answers

download txt file automatically from django

In the following code, if i change the file output format to output.csv the file gets automatically downloaded ,but if i change the format to output.txt the files gets displayed on the browser.How to auto download the output.txt file Since csv file…
Rajeev
  • 44,985
  • 76
  • 186
  • 285
3
votes
1 answer

Django Update/Delete view. Handling user permissions

models.py class Punch(models.Model): ro_number = models.IntegerField() flag = models.FloatField(max_length=10) actual = models.FloatField(max_length=10) description = models.CharField(max_length=100, blank=True) user =…
3
votes
1 answer

How to redirect a Django template and use the 'Next' variable?

I am using Django's built in login view as can be seen in the following snippets: urls.py url(r'^login$',login,{'template_name': 'login.html'},name="login"), template.html
{% csrf_token…
Mohammed B
  • 305
  • 1
  • 4
  • 14
3
votes
1 answer

django unit test for ModelChoiceField form with CheckboxSelectMultiple widget

I have a form class TypesForm(forms.Form): .... types = forms.ModelChoiceField( label='Types', queryset=models.Type.objects.all(), widget=forms.CheckboxSelectMultiple) ... How do I write a unit test for this form, when I want to…
WojtylaCz
  • 328
  • 2
  • 12
3
votes
1 answer

Django UpdateView forms

I have a form class that looks like.. #forms.py class ExampleForm(forms.Form): color = forms.CharField(max_length=25) description = forms.CharField(widget=forms.Textarea, max_lenght=2500) and my view looks like this.. #views.py class…
L.hawes
  • 189
  • 3
  • 14
1 2 3
99
100