django-views

Combine prefetch_related and annotate in Django

*爱你&永不变心* 提交于 2019-12-10 16:27:40
问题 I have three models class ModelA(models.Model): name = CharField(max_length=100) class ModelB(models.Model): modela = ForeignKey(ModelA) class ModelC(models.Model): modelb = ForeignKey(ModelB) amount = IntegerField() I can get the output name, number of model c objects ============== Some name, 312 Another name, 17 With the queryset ModelA.objects.all().prefetch_related('modelb_set', 'groupb_set__modelc_set') and template {% for modela in modela_list %} {% for modelb in modela.modelb_set.all

Django: A more DRY way to prevent edit/delete of objects?

瘦欲@ 提交于 2019-12-10 16:25:39
问题 After reading the permission Django documentation, I'm still confused. I'd like to prevent access for user to edit or delete objects they didn't own. I dit it this way and it works: In views.py: def deleteReward(request, reward_id): reward = get_object_or_404(Reward, pk=reward_id) if reward.owner.user != request.user: # if the user linked to the reward is not the current one raise Exception("This reward is not yours, you can't delete it !") #... But I think this isn't clean and DRY for two

data format value changes in database

≯℡__Kan透↙ 提交于 2019-12-10 16:21:46
问题 forms.py DATE_INPUT_FORMAT = ( ('%d/%m/%Y','%m/%d/%Y') ) class ReportForm(forms.ModelForm): manual_date = forms.DateField(input_formats = DATE_INPUT_FORMAT, widget=forms.DateInput(format = '%d/%m/%Y')) 1.Date format should change depend upon the value in database,if value is in db,it shows the 1st format and for none ,else part is executing. 2.Formats are changing depends upon the condition. 3.I am facing problem here,if the input format is of this (%m/%d/%Y),on form post the value of date

Django view response time issues

谁说胖子不能爱 提交于 2019-12-10 15:53:11
问题 Hi i have lots of objects which i get from query, the queryset is not yet evaluated, when i pass objectlist to paginator object it took 14 seconds to return paginator object, it is because it is evaluating the all objects in list which took time (hitting db may be). I forcefully evaluated queryset before sending it to paginator object as: ds_objects = list(ds_objects) Guess what now pagination took 0.0 seconds in returning object, but the problem still remain now all time is taken by ds

How does one protect his AJAX views with Django?

拈花ヽ惹草 提交于 2019-12-10 15:28:56
问题 I'm using a private view in my Django project for an AJAX request. def HereIsSomeJSON(request, label): if not request.method == "POST": raise PermissionDenied # Here is the job of my AJAX, basically feeding a JSON json = {...} return HttpResponse(json, "application/json") Using JavaScript, I request for the AJAX with jQuery as so: function FeedMeWithJSON() { // Django needs his Cross-Site Request Forgery token to welome POST datas oPostDatas = { 'csrfmiddlewaretoken': '{{ csrf_token }}' };

Why is my Django view being hit twice with every page view?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:57:51
问题 I can't seem to find the problem for the life of me. Very simply, I have a database object that I'm pulling from the database, incrementing it's "views" by one, and saving. My view display's the incremented value, but then my logs show that the value is incremented AGAIN. g=Game.objects.filter(slug=slug).distinct()[0] g.views += 1 g.save() Here's my logs: [Fri Oct 29 15:15:49 2010] [error] DEBUG:root:Updating plays [Fri Oct 29 15:15:49 2010] [error] DEBUG:root:plays: 40 [Fri Oct 29 15:15:50

Django: Send HTML email via send_mass_mail()

泪湿孤枕 提交于 2019-12-10 14:49:30
问题 It seems from the docs on email that HTML is supposed for send_mail() but not send_mass_mail() . Is my understanding correct, and if so is there a work-around to get the send_mass_mail() functionality with HTML without writing a custom loop? https://docs.djangoproject.com/en/1.7/topics/email/ 回答1: You should check this answer where the guy creates a customized send_mass_mail() function that accepts HTML working with EmailMultiAlternatives 来源: https://stackoverflow.com/questions/26378008

django slice string in template

天大地大妈咪最大 提交于 2019-12-10 13:17:38
问题 index.html <td>{% if place or other_place or place_description%}{{ place}} {{ other_place}} {{place_description}}</td> This is displaying all the data in template.I want to truncate the string if it is more than length 80. Conditions are, 1.If place variable have more than 80 character,it should truncate their and need not show the other two variable like other_place and place_description. 2.If place variable and other_place variable making more than 80 character,in this case it should

edit and update the row in database using django

两盒软妹~` 提交于 2019-12-10 12:19:12
问题 models.py class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField() age = models.IntegerField() def __unicode__(self): return "{0} {1} {2} {3} {4}".format( self, self.first_name, self.last_name, self.email, self.age) class Book(models.Model): book_name=models.CharField(max_length=30) publisher_name=models.CharField(max_length=40) author=models.ForeignKey(Author) def __unicode__(self): return "{0} {1} {2}"

Using APIs within Django and how to display the data

穿精又带淫゛_ 提交于 2019-12-10 12:13:36
问题 I am trying to test how to display API information within a view on my Django project. I know you may have to add some installed APIs into the settings INSTALLED APPS block. This api is a simple geo one. I am new to Django and new to using APIs within it. I have managed to get my app the way I need it using Youtube videos. But now I am on my own. I have many different view classes to display differents of my app. The view below is the view Id like to place the data on. is this how I would