django-views

Django: Accessing Logged in User when specifying Generic View in urlpatterns

倖福魔咒の 提交于 2019-12-23 16:12:54
问题 I have a model that looks like this: from django.db import models from django.contrib.auth.models import User class Application(models.Model): STATUS_CHOICES = ( (u'IP',u'In Progress'), (u'C',u'Completed')) status = models.CharField(max_length=2 ,choices=STATUS_CHOICES, default='IP') title = models.CharField(max_length = 512) description = models.CharField(max_length = 5120) principle_investigator = models.ForeignKey(User, related_name='pi') And I want to use a generic ListView that lists the

Refresh template in Django

喜欢而已 提交于 2019-12-23 13:01:07
问题 I have a view like this: def form1(request): if request.method == 'POST': form = SyncJobForm(request.POST) if form.is_valid(): # do something in_progress = True return render_to_response('form1.html', {'in_progress': in_progress}) I would like to know how to set it to refresh the template after is done with the view process. Like rendering the page after its done: in_progress = True return render_to_response('form1.html', {'in_progress': in_progress}) # after its finished finished = True

Django not like statement

流过昼夜 提交于 2019-12-23 12:08:09
问题 how to use not like in django queries Model.objects.filter(keywords not like "null" or "undefined") select * from model where keywords not like "%undefined%" or keywords not like "%null%"; 回答1: use the exclude function and Q objects Model.objects.exclude(Q(keyword__contains='undefined') | Q(keyword__contains='null')) 来源: https://stackoverflow.com/questions/4193097/django-not-like-statement

Overriding the Django admin change_list_results.html only for some models

孤人 提交于 2019-12-23 10:53:17
问题 I want to override the templates/admin/change_list_results.html and templates/admin/change_list.html templates for just one of my models. How do I tell the admin to differentiate this model from all the others in my app and render a different change template than the default? If I just edit change_list_results.html then all the models in the admin views will reflect my changes. 回答1: You can, see documentation here. The short version is that you need a custom template at admin/your-app-name

Is it possible to reload the view without restarting Django?

假装没事ソ 提交于 2019-12-23 10:48:10
问题 After changing the view function without runserver again, and press F5 to refresh the page, Django will not reload the new view but use the previous one. but if you change the template, Django always uses the new one. So, Is there a way to make Django reload the view every time the user refresh the page, I think that is very convenient for develop to modify the view function frequently. 回答1: If you are running django using the dev server (./manage.py runserver) then it will always reload when

Django 2.0 url() to path()

余生长醉 提交于 2019-12-23 10:33:07
问题 I am currently learning Django. Until now I was working with Django 1.1 but now I am working with Django 2.0. Django 2.0 uses path() instead of url() and I don't quiet understand that. In Django 1.1 my urls looked like this: url(r'^about/$', views.AboutView.as_view(), name='about'), Now with Django 2 it looks like this path('about/', views.AboutView.as_view(), name='about'), So far so good but I just don't undertand how I can convert this url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as

django rest nested relation in post/put

北城余情 提交于 2019-12-23 09:32:58
问题 I am new in django rest api developement. I have two models one is category and another is subcategories. Here is my models class Category(models.Model): title = models.Charfield() brief = models.TextField() subcategories = model.ManyToManyField('Subcategory', blank=True) My serializer class class CategorySerializer(serializers.ModelSerializer): title= serializer.Charfield() subcategories = Relatedfield(many=True) Now in view def post(self, request, format = None): data=request.DATA serialize

Django filter query from many to many fields

旧街凉风 提交于 2019-12-23 06:39:29
问题 models.py class Keyword(models.Model): name=models.CharField(max_length=500,unique=True) image = models.ImageField(upload_to='keywords/', blank=True, null=True) mood=models.ManyToManyField(Mood,blank=True) def __str__(self): return str(self.name) class ItemVariation(models.Model): restaurant=models.ForeignKey(Restaurant,on_delete=models.CASCADE) items_id=models.ForeignKey(Item,on_delete=models.CASCADE) price=models.IntegerField(blank=True,null=True,default=0) item_code=models.CharField(max

Django filter query from many to many fields

你离开我真会死。 提交于 2019-12-23 06:39:11
问题 models.py class Keyword(models.Model): name=models.CharField(max_length=500,unique=True) image = models.ImageField(upload_to='keywords/', blank=True, null=True) mood=models.ManyToManyField(Mood,blank=True) def __str__(self): return str(self.name) class ItemVariation(models.Model): restaurant=models.ForeignKey(Restaurant,on_delete=models.CASCADE) items_id=models.ForeignKey(Item,on_delete=models.CASCADE) price=models.IntegerField(blank=True,null=True,default=0) item_code=models.CharField(max

Django filter query from many to many fields

和自甴很熟 提交于 2019-12-23 06:39:07
问题 models.py class Keyword(models.Model): name=models.CharField(max_length=500,unique=True) image = models.ImageField(upload_to='keywords/', blank=True, null=True) mood=models.ManyToManyField(Mood,blank=True) def __str__(self): return str(self.name) class ItemVariation(models.Model): restaurant=models.ForeignKey(Restaurant,on_delete=models.CASCADE) items_id=models.ForeignKey(Item,on_delete=models.CASCADE) price=models.IntegerField(blank=True,null=True,default=0) item_code=models.CharField(max