django-views

Totals/Subtotals in Django template

好久不见. 提交于 2019-12-24 11:26:51
问题 I'm having difficulty wrapping my head around a simple problem. How do you calculate totals and subtotals in a django template? Let's say I want to generate a report of customer's orders, something like: Desired Report Output Customer1 1 Widgets $ 2 1 Bobbins $ 1 Subtotal $ 3 Customer2 2 Widgets $ 4 2 Bobbins $ 2 Subtotal $ 6 TOTAL $ 9 Let's assume we populate a dictionary in our view orgs = {} orgs['Customer1'] = [ { 'qty': 1, 'descr' : 'Widgets', 'price': 2 }, { 'qty': 1, 'descr' : 'Bobbins

django-userena removing mugshot

依然范特西╮ 提交于 2019-12-24 11:00:04
问题 I am building a site which uses userena for the profile and registration part. The problem is that I am trying to remove the mugshot upload part and the profile privacy(registered,open,closed) from edit profile page so that userena uses gravatar only and the profiles are public for all. But in the template there is just <fieldset> <legend>{% trans "Edit Profile" %}</legend> {{ form.as_p }} </fieldset> <input type="submit" value="{% trans "Save changes" %}" /> </form> I am trying to find out

error 'NoneType' object has no attribute '__dict__'

╄→尐↘猪︶ㄣ 提交于 2019-12-24 10:44:35
问题 I have encoutered this error and its not letting me save the info in the form. The initial data is showing well in the form but saving is challenging me. Hope someone can help, I'm really stuck class UserPostCreatView(CreateView): form_class = PostModelForm template_name = 'posts/post_form.html' success_url = "/profile/{user_slug}/wall" def get_initial(self): # Get the initial dictionary from the superclass method initial = super(UserPostCreatView, self).get_initial() user_slug = self.kwargs

Django-Countries: wrong sorting in translated choices (but works in the admin)

試著忘記壹切 提交于 2019-12-24 10:39:53
问题 I use Django Countries like the following. In my view it seems to order the select items according to the english original values (e.g. Deutschland will be found under G (=Germany)) but in the admin the values are ordered according to the current language. This isn't done by JavaScript (I tried it by disabling JS). I have no idea how to fix this. Versions: Django 1.5.5, Django Countries 2.1.2 models.py from django_countries.fields import CountryField class MyModel(ModelSubClass): country =

Using GeoJson data format how can we write dynamic query for “properties dictionary fields ”?

╄→гoц情女王★ 提交于 2019-12-24 10:22:40
问题 I have data in geojson format. Here is my data : { "type" : "FeatureCollection", "features" : [ { "type" : "Feature", "properties" : { "year" : 2015, "season" : "rabbi", "crop" : "banana", "district" : "pune", "taluka" : "haveli", "circle" : "uralikanchan", "farmer" : 100 }, "geometry" : { "type" : "Polygon", "coordinates" : [ [ 74.129992, 18.505494 ], [ 74.129047, 18.505494 ], [ 74.128275, 18.504436 ], [ 74.127588, 18.503052 ], [ 74.114456, 18.498331 ], [ 74.113941, 18.498331 ], [ 74.112482,

Whats the most Django/pythonic way to create or overwrite a record?

99封情书 提交于 2019-12-24 09:58:58
问题 Working with Django 1.2 I am making a wine review site. A user should only be able to review each wine once, but should be able to go back and re-review a wine without raising an error. Using the get_or_create method seems the most rational solution but I have been running into various problems implementing it. Searching I found this article which looked promising: Correct way to use get_or_create? and of course the django documentation on it: http://docs.djangoproject.com/en/1.2/ref/models

Django: AttributeError - Object has no attribute

安稳与你 提交于 2019-12-24 09:24:18
问题 This view is throwing the AttributeError saying :'PhotoForm' object has no attribute 'reservation'. What happens- the function passes, but doesn't actually upload the image. In debugging it, the form is not valid. I tried printing form.reservation form.message form.photo But that shows this traceback ( which is the reason why the form isn't valid & not completing the function. As I built this off other functions that are working, I'm a bit confused. Thanks for the help! The HTML Form <form

serialize django model queryset with serializers.serialize() function

淺唱寂寞╮ 提交于 2019-12-24 08:59:36
问题 How can I return JSON response of model queryset from a view using django serializer ? from django.core import serializers from django.http.response import JsonResponse def some_view(request): qs = SomeModel.objects.all() serialized_obj = serializers.serialize('json', qs) return JsonResponse(serialized_obj, safe=False) According to code snippet, the view producess a non-json response. 回答1: This can be easily done by using python format. serialized_obj = serializers.serialize( 'python' , qs)

Why do we use form.save(commit=False) in Django-views?

那年仲夏 提交于 2019-12-24 07:50:46
问题 I am unable to understand the reason why we use form.save(commit=False) instead of simply using form.save in Django-views . Could someone explain me the difference and the necessities on both? 回答1: form.save(commit=False) is mostly used if you are working with ModelForm. The main use case is if you have a ModelForm that doesn't contains all the required field of a model. You need to save this form in the database, but because you didn't give it all the required fields you will get an error.

Django views and forms submit button update database

我怕爱的太早我们不能终老 提交于 2019-12-24 07:38:20
问题 This is the form that the submit button is in. <form> <button type="submit" name="subscribe" class="btn btn-primary pull-right">Subscribe</button></h5> </form> And I have this code in my view: def tour_sub(request): tour = Tour.objects.filter(id=1) if 'subscribe' in request.POST: tour.subscribers.add(user) tour.save() When the subscribe button is clicked I just want to update the record and insert that in the database. But nothing happens when I click the Subscribe button. I am new in django