django-views

Refreshing table with Dajaxice in Django

时光毁灭记忆、已成空白 提交于 2019-12-04 17:13:37
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_data = current_object.Data temperature_dict[filter_device] = current_data return render_to_response(

book count per author for filtered book list in django

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 16:41:25
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 ... Let's build the query step by step. First, get the authors who have a book in book_list . authors = Author.objects.filter(book__in=book_list) The trick is to realise that an author will appear once for

How to display custom 404.html page in Django

做~自己de王妃 提交于 2019-12-04 15:48:26
问题 I want to display custom 404 error page when end user enters wrong url,I have tried but i am getting only Django default 404 page.I am using Python(2.7.5),Django(1.5.4) My Code urls.py from django.conf.urls import patterns, include, url from mysite import views handler404 = views.error404 urlpatterns = patterns('', url(r'^$', 'mysite.views.home', name='home'), ) views.py from django.http import HttpResponse from django.shortcuts import render from django.template import Context, loader def

How to hide a field in django modelform?

与世无争的帅哥 提交于 2019-12-04 15:44:30
For example: class TestModel(models.Model): ref1 = models.ForeignKey(RefModel) text1 = models.TextField() class TestModelForm(ModelForm): class Meta: model = TestModel fields = ('text1') I only allow the user to input text1 field, but when I redefine the post method of my view, I also want to set the ref1 value, how should I do that? I wish I can let TestModelForm has the ref1 field but don't let user modify it, then I can modify the value of request.POSt in post method, and pass it to TestModelForm, is that possible? You can use HiddenInput as ref1 widget: class TestModelForm(ModelForm):

What's the difference between returning a `HttpResponseNotFound` and raising a `Http404` in Django?

怎甘沉沦 提交于 2019-12-04 15:37:45
问题 There are apparently two different ways to return a 404 error in Django: by returning a HttpResponseNotFound object or by raising an Http404 exception. While I'm using the former in my project, it seems that Django's internal views are mostly using the latter. Apart from the "Exception is exceptional" mantra, what's the difference between both ways and which should I be using? 回答1: An HttpResponseNotFound is just like a normal HttpResponse except it sends error code 404. So it's up to you to

Django: how to process flat queryset to nested dictionary?

♀尐吖头ヾ 提交于 2019-12-04 15:19:12
I have a table with data looking like this: |Country|State|City |Street| |-------|-----|-----|------| | USA | AZ |city1| str1 | | USA | AZ |city1| str2 | | USA | AZ |city2| str1 | | USA | AZ |city2| str3 | | USA | MN |city3| str4 | | MEX | CH |city4| str5 | | MEX | CH |city4| str6 | What is a proper way to convert this into nested dictionary? I expect the result looking like this: nested_dict = { 'USA':{ 'AZ':{ 'city1':['str1','str2'], 'city2':['str1','str3'], }, 'MN':{ 'city3':['str3','str4'], }, }, 'MEX':{ 'CH':{ 'city4':['str5','str6'], }, }, } You can use a nested defaultdict : from

Django Primary Key: badly formed hexadecimal UUID string

会有一股神秘感。 提交于 2019-12-04 15:06:21
I'm trying to use UUIDField as a primary key for a model. I'm using CreateView for creating objects for this model. Anytime I browse to the url for creating one of the objects I get the error: badly formed hexadecimal UUID string The stack trace shows the error occurs here, where value is created: /home/conor/django/venv2/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_db_prep_value return name, path, args, kwargs def get_internal_type(self): return "UUIDField" def get_db_prep_value(self, value, connection, prepared=False): if isinstance(value, six.string_types):

Issues with feeding data into database when using for loop

喜你入骨 提交于 2019-12-04 15:03:52
In my template I have used for loop for some fields <div class="register_div"> <p>Title:</p> <p>{{ form.title }}</p> </div> <div class="register_div"> <p>Upload Images :</p> <p>{{ form.image }}</p> </div> {% for item in product %} <div class="register_div"> <p>{{ item.Name }} <input type="text" name="custom[{{item.id}}]"/></p> </div> {% endfor %} <div class="register_div"> <p>Price:</p> <p>{{ form.price }}</p> </div> As code shows there is one field which using for loops if that field has three records then in the form it shows three text boxes so that user can feed data to all three fields,

Create django object using a view with no form

情到浓时终转凉″ 提交于 2019-12-04 15:03:47
I was wondering how I would be able to create an object in a database based the URL a user is going to. Say for example they would go to /schedule/addbid/1/ and this would create an object in the table containing the owner of the bid, the schedule they bidded on and if the bid has been completed. This here is what I have for my model so far for bids. class Bids(models.Model): id = models.AutoField("ID", primary_key=True, editable=False,) owner = models.ForeignKey(User) biddedschedule = models.ForeignKey(Schedule) complete = models.BooleanField("Completed?", default=False) The biddedschedule

How to cache a paginated Django queryset

南楼画角 提交于 2019-12-04 14:38:40
问题 How do you cache a paginated Django queryset, specifically in a ListView? I noticed one query was taking a long time to run, so I'm attempting to cache it. The queryset is huge (over 100k records), so I'm attempting to only cache paginated subsections of it. I can't cache the entire view or template because there are sections that are user/session specific and need to change constantly. ListView has a couple standard methods for retrieving the queryset, get_queryset() , which returns the non