django-views

Passing request.user to template in Django

一个人想着一个人 提交于 2019-12-06 01:15:42
Is there another way to get the request.user by not passing it from the views? I think passing request.user from all functions in views to the template is quite wrong. Is there any method or way that the template will get the user or any object in the database? By default (I am talking about Django version 1.3) you do not need change TEMPLATE_CONTEXT_PROCESSORS. Because default value already contains *django.contrib.auth.context_processors.auth*. So to your question: By default, you should be able to use user , messages and perms variables in your template. For example: User: {{user.username}}

Optimize code in Django - view ManyToMany as matrix

烂漫一生 提交于 2019-12-06 00:48:33
I'm trying to show user group permissions in Django and show them in a "Drupal" style like a matrix. It works, but it takes too long to make the query and paint it in the template. Is there some way to improve my code? view img up(accomplished),down(views and template.html) views : def GroupPermissionsView(request): title = "Groups Permissions" groups = Group.objects.all() permissions = Permission.objects.all() context = Context({ 'title': title, 'groups': groups, 'permissions': permissions, }) return render( request, 'forms_permissions.html', context ) template: <table class="table table

Django async send notifications

故事扮演 提交于 2019-12-06 00:44:55
I have a notifications for a users when the admin add notification to user i want to display on user's home page like a facebook likes or activity feed is it possible with django ? models.py class Notification(BaseModel): created_user = models.ForeignKey(User) title = models.CharField(max_length=225) description = models.TextField(max_length=600) is_read = models.BooleanField(default=False) priority = models.CharField(max_length=20, choices=NOTIFICATION_STATUS, default=LOW) def __str__(self): return self.title i'm able to add a new notificiation but the notifications listing when the user

Load Django Form Object with data from Model instance already “loaded”

China☆狼群 提交于 2019-12-06 00:29:37
I have the following type of template in Django: <form .... > <label class="control-label" for="id_quantity">Quantiy</label> {{ form.quantity }} <label class="control-label" for="id_title">Quantiy</label> {{ form.title}} </form> The view contains a ModelForm object in its context from which the "form" variable in the template is derived. Often, you can pass a form that is bound to data like request.POST. How do I load a form that is bound to the data of a specific instance of a model? Is there any special syntax? There are two options here: MyModelForm(instance=myInstanceModel) MyModelForm

Query of a subquery in Django

倖福魔咒の 提交于 2019-12-06 00:07:14
I'm trying to do a query from another query, but Django said: 'Caught DatabaseError while rendering: subquery returns more than 1 row.' I'm using PostGis. my model class Place(models.Model): coordinate = models.PointField() class TranslatedPlace(models.Model): place = models.ForeignKey(Place) my view near_coordinates = Place.objects.filter(coordinate__distance_lte=(place_obj.coordinate, D(km=100))) near_places = TranslatedPlace.objects.filter(place=near_coordinates) I believe you'll want to use in to filter the second queryset near_coordinates = Place.objects.filter(coordinate__distance_lte=

The QuerySet value for an exact lookup must be limited to one result using slicing-Django

拟墨画扇 提交于 2019-12-05 20:54:57
I'm building a news website.While I tried to get the list of relative news which have the same tags.The error said: The QuerySet value for an exact lookup must be limited to one result using slicing-Django. I have two models News and Tag,Tag is a many to many foreign key of News. News model: class News(models.Model): tag = models.ManyToManyField(Tag, blank=True, verbose_name='tag') Tag model: class Tag(models.Model): name = models.CharField(max_length=40) View: def newsDetailView(request, news_pk): news = get_object_or_404(News, id=news_pk) tags = news.tag.annotate(news_count=Count('news'))

Django - Error models with control data

自闭症网瘾萝莉.ら 提交于 2019-12-05 20:31:42
I'm working on writing the models.py file, and I need some controls to perform the automatic compilation of some fields. MOTIVOINGRESSO = ( (u'sequestro ', u'sequestro'), (u'fermo ', u'fermo'), (u'confisca final ', u'confisca final'), (u'cambio custodian ', u'cambio custodian'), ) motivo_ingresso = models.CharField (max_length = 50, choices = MOTIVOINGRESSO) FERMO = ( (u'30 ', u'30'), (u'60 ', u'60'), (u'90 ', u'90'), (u'180 ', u'180'), (u'1 month ', u'1 month'), (u'3 months, 'u'3 months'), (u'indeterminato ', u'indeterminato'), ) durata_in_giorni_del_fermo = models.CharField (max_length = 20,

Django: validating unique_together constraints in a ModelForm with excluded fields

不羁岁月 提交于 2019-12-05 20:30:32
I have a form: class CourseStudentForm(forms.ModelForm): class Meta: model = CourseStudent exclude = ['user'] for a model with some complicated requirements: class CourseStudent(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL) semester = models.ForeignKey(Semester) block = models.ForeignKey(Block) course = models.ForeignKey(Course) grade = models.PositiveIntegerField() class Meta: unique_together = ( ('semester', 'block', 'user'), ('user','course','grade'), ) I want the new object to use the current logged in user for CourseStudent.user: class CourseStudentCreate(CreateView):

Django ModelChoiceField drop down box custom population

倾然丶 夕夏残阳落幕 提交于 2019-12-05 19:43:37
I have a dropdown box that is being populated by a filtered list of objects from a model "Options". Currently, the dropdown list displays the names of each option. How would I get it to display another attribute from the same table? self.fields['name'] = forms.ModelChoiceField(queryset = Options.objects.filter(option_type = s), label = field_label, required=False) Quick example: drop down box currently displays the names of the cars: "Camero, Nissan, Honda" How would I get it to display the color of each car ("black, black, white"). Note that the color is also a field in the Option table. You

django sort the query elements in a weekly monthly daily fashion

六月ゝ 毕业季﹏ 提交于 2019-12-05 18:29:21
I am getting a list of people and the tests they have taken from an api in the same project. I would like the user to have an option to see the number of tests that have taken place in a city with three options - daily/weekly/monthly. models.py class City(models.Model): city_name=models.CharField(max_length=100,default='',blank=False) class Person(models.Model): title = models.CharField(max_length=3,default="mr",blank=False) name = models.CharField(max_length=50,default='',blank=False) address = models.CharField(max_length=200,default='',blank=False) city = models.ForeignKey(City) class Test