django-views

django remove unicode from query result

冷暖自知 提交于 2019-12-04 20:02:48
Django query gives me below output format,but i want below format data=`[{'total': 1744, 'name: u'x'}, {'total': 13, 'name': u'm'}, {'total': 126, 'role': name'n'}]` m=[] for i in data: m.append(i.values()) print m it give me output [[1744,u'x'], [13,u'm'], [126,u'n']] but i need output in how to remove unicode symbol from output [['x',1744], ['m',13], ['n',126]] how to do this ? Thanks in advance Try this: >>> import json >>> data=[{'total': 1744, 'name': u'x'}, {'total': 13, 'name': u'm'}, {'total': 126, 'name': u'n'}] >>> json.dumps([i.values()[::-1] for i in data]) '[["x", 1744], ["m", 13]

Can I have multiple lists in a Django generic.ListView?

限于喜欢 提交于 2019-12-04 19:51:23
问题 As a Django beginner I'm working on the the tutorial provided by django docs at https://docs.djangoproject.com/en/1.5/intro/tutorial04/ In it they demonstrate a list of multiple polls that are listed using a query by publication date. Could I add another list to be also used in the template to be used as well. Example Displaying a list of latest polls by date and another by alphabetical order on the same page. class IndexView(generic.ListView): template_name = 'polls/index.html' context

Overriding Django views with decorators

点点圈 提交于 2019-12-04 19:46:45
I have a situation that requires redirecting users who are already logged in away from the login page to another page. I have seen mention that this can be accomplished with decorators which makes sense, but I am fairly new to using them. However, I am using the django login and a third party view (from django-registration). I do not want to change any of the code in django.contrib.auth or django-registration. How can I apply a decorator to a view that is not to be modified in order to get the desired behavior. Thanks in advance! UPDATE: I discovered that I mistakenly associated the login

How to use context with class in CreateView in django?

淺唱寂寞╮ 提交于 2019-12-04 19:34:46
How to use context with class in CreateView in django? Before i have: #views.py from django.views.generic import CreateView from cars.models import * def CreateCar(CreateView): info_sended = False if request.method == 'POST': form = FormCar(request.POST, request.FILES) if form.is_valid(): info_sended = True form.save() else: form = FormCar() ctx = {'form': form, 'info_sended':info_sended} return render_to_response("create_car.html", ctx, context_instance=RequestContext(request)) Now, a have, and try: class CreateCar(CreateView): info_sended = False template_name = 'create_car.html' model = Car

Get Django views.py to return and execute javascript

不问归期 提交于 2019-12-04 19:33:06
问题 So I'm working with django and file uploads and I need a javascript function to execute after the file has been uploaded. I have a file upload handler in my views.py which looks like this: def upload_file(request): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): for f in request.FILES.getlist('fileAttachments'): handle_uploaded_file(f) return HttpJavascriptResponse('parent.Response_OK();') else: return HttpResponse("Failed to upload attachment.") And I found a django

Bootstrap3 tabs in Django

社会主义新天地 提交于 2019-12-04 19:08:19
I want to implement Bootstrap3 tabs in my app, which displays school data by state. So if you go to example.com/ma/ you will see information for the state of Massachusetts and tabs to sort by grade level. I am already using the queryset to filter by state so that on example.com/ma/ only "ma" results appear. And I can show ALL data in one of the tabs, but can't filter it out for multiple tabs. To keep it simple, I just want to do tabs for "All" and "High School" here. Here is my models.py : from django.db import models class School(models.Model): school_name = models.CharField(max_length=200)

Update view using function based view

痴心易碎 提交于 2019-12-04 19:02:27
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. 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', {'form':form}) coderknight439 Here, I tried something.... def approveform(request, pk): if pk: form =

django/taggit - unhashable type: 'list'

送分小仙女□ 提交于 2019-12-04 18:45:40
I'm using django-taggit (see here ). This is what I have: forms.py from taggit.forms import * class MyForm(forms.Form): title = forms.CharField() my_tags = TagField(max_length=800, widget=forms.TextInput(attrs={'class':'myTags'})) views.py if 'submit_button' in request.POST: form = MyForm(request.POST) if form.is_valid(): cd = form.cleaned_data f_title = cd['title'] f_my_tags = cd['my_tags'] p = MyData.objects.create(title=f_title) p.tags.add(f_my_tags) p.save() mytemplate.html {{ form.my_tags.errors }} {{ form.my_tags }} Not sure why I get unhashable type: 'list' when I use p.tags.add(f_my

Django: List Products of each Categories in a page

▼魔方 西西 提交于 2019-12-04 18:17:24
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): return reverse('category_detail', kwargs={'slug': self.slug}) @property def get_products(self):

Django view getting called twice (double GET request)

懵懂的女人 提交于 2019-12-04 17:21:57
I'm creating a classifieds website in Django. A single view function handles global listings, city-wise listings, barter-only global listings and barter-only city-wise listings. This view is called ads . The url patterns are written in the following order (note that each has a unique name although it's tied to the same ads view): urlpatterns = patterns('', url(r'^buy_and_sell/$', ads,name='classified_listing'), url(r'^buy_and_sell/barter/$', ads,name='barter_classified_listing'), url(r'^buy_and_sell/barter/(?P<city>[\w.@+-]+)/$', ads,name='city_barter_classified_listing'), url(r'^buy_and_sell/