django-views

Update view using function based view

别来无恙 提交于 2019-12-06 14:29:25
问题 How can I pass an object into a model form to pre-populate the field when the page is rendered? I want to do something similar to the build in Django UpdateView class based view but with a function based view. 回答1: Just get the object from model and pass that object as instance to the form. Then pass the form to the template. Write your view like below example. def func(request, id): object = Model.objects.get(id=id) form = ModelForm(instance=object) return render(request, 'my_template.html',

django - get list of objects by filtering a list of objects

馋奶兔 提交于 2019-12-06 14:22:32
I am creating a user activity streams. models for activity: class Activity(models.Model): actor = models.ForeignKey(User) action = models.CharField(max_length=100) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') pub_date = models.DateTimeField(auto_now_add=True, auto_now=False) model for Relationship: class Person(models.Model): user = models.OneToOneField(User) relationships = models.ManyToManyField('self', through='Relationship', symmetrical=False, related_name='related_to')

File upload with Django: Invalid form

人走茶凉 提交于 2019-12-06 14:21:32
First time posting a question. I've basically copied everything I've found on here and on the django documentation site and I can't figure out what I'm doing incorrectly Here is the code from my views.py file from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() def upload_file(request): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): response = "Form is valid." else: response = "Failed to upload attachment." return HttpResponse(response) And my html file: <form name="attachmentForm" action="http:/

Django: List Products of each Categories in a page

本小妞迷上赌 提交于 2019-12-06 14:12:38
问题 I have a few categories and I would like to list the products per category in the format below (categories is an FK to products): Category 1 bunch of products .... Category N bunch of products I have tried many ways but so far I only get the categories but not the products to show in my HTML. models.py class Category(models.Model): title = models.CharField(max_length=225) slug = models.SlugField(unique=True, blank=True, null=True) def __str__(self): return self.title def get_absolute_url(self

Django: How to include a view from within a template

瘦欲@ 提交于 2019-12-06 13:29:18
I'm new to django and I was wondering what is the best/recommend approach to include a view (with certain logic, and its resulting HTML) from within a template. My concrete example is as follows: I have a model which is: class City(models.Model): name = models.CharField(max_length=200) class Place(models.Model): city = models.ForeignKey(City) name = models.CharField(max_length=200) state = models.IntegerField() So I need a view to show each city and it's places. Each place should be rendered in a different way depending on its state. Each different way is very different and will probably need

Baffled: Django “could not import app.views” but can import app, in WSGI?

帅比萌擦擦* 提交于 2019-12-06 11:49:43
问题 I'm having an odd Django problem, running Django with mod_wsgi. Django is finding urls.py , and then saying: ViewDoesNotExist: Could not import app.views. Error was: No module named views Bizarrely, if I have import app in the import statements in urls.py I don't get an error (until it hits app.views as above), but if I have from app import views in my import statements, I get an error. My python path, as shown in the Django debug info, has both the containing folder and the app folder on it.

Only show the latest 3 posts in Django?

廉价感情. 提交于 2019-12-06 11:49:13
问题 I've got a blog app in Django. I want to show the latest 3 blog posts on another page but can only show all of the blog posts ordered by date. How can I show the latest 3 posts? Do I filter this in the views or on the template tags? models.py from django.db import models from django.core.urlresolvers import reverse # Create your models here. class Post(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique=True, max_length=255, null=True) pub_date = models

book count per author for filtered book list in django

旧街凉风 提交于 2019-12-06 11:37:59
问题 Short question. I have two models: class Author(models.Model): name = models.CharField(max_length=250) class Book(models.Model): title = models.CharField(max_length=250) author = models.ManyToManyField(Author) One view: def filter_books(request): book_list = Book.objects.filter(...) How can I display in template next content: Authors in selected books: Author1: book_count Author2: book_count ... 回答1: Let's build the query step by step. First, get the authors who have a book in book_list .

Use serializer of model having foreign key to do CRUD on parent table in Django Rest Framework

落花浮王杯 提交于 2019-12-06 11:32:28
In my API, I have two models Question and Option as shown below class Question(models.Model): body = models.TextField() class Options(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) option = models.CharField(max_length=100) is_correct = models.SmallIntegerField() While creating a question it would be nicer the options can be created at the same time. And already existed question should not be created but the options can be changed if the options are different from previous. I am using ModelSerializer and ModelViewSet . I use different urls and views for Question

Refreshing table with Dajaxice in Django

杀马特。学长 韩版系。学妹 提交于 2019-12-06 11:12:29
问题 I'm monitoring the temperature for different locations. I have the data stored in a model and have set my views.py, but I would like to refresh the table every 5 minutes. I'm new to ajax and dajaxice, how can I write the function so it displays in html? this is my views.py: def temperature(request): temperature_dict = {} for filter_device in TemperatureDevices.objects.all(): get_objects = TemperatureData.objects.filter(Device=filter_device) current_object = get_objects.latest('Date') current