django-templates

Django: UnboundLocalError: local variable 'company' referenced before assignment

百般思念 提交于 2019-12-02 12:29:17
问题 I am trying to make a url field in my detail view by passing two primary key in it... This is what I have done in urls.py: url(r'^company/(?P<pk1>\d+)/groupdetail/(?P<pk2>\d+)/$',views.group1DetailView.as_view(),name='groupdetail'), And in my views: def get_object(self): pk1 = self.kwargs['pk1'] pk2 = self.kwargs['pk2'] company = get_object_or_404(company, pk=pk1) group1 = get_object_or_404(group1, pk=pk2) return group1 I am getting error in this line: company = get_object_or_404(company, pk

How to create link for one of the category in Django

谁说胖子不能爱 提交于 2019-12-02 11:47:15
I'm coding a news website.I have 'category' in News model. I want to get all the news in one of the categories named 'opinion' in my index.html. And create detail page link for each of them. I can the title ,author, etc of the news mentioned above .But my brain really junks,I don't know how to create a link points to opinion_new.html or news_detail.htlm for each of them.I have a link for regular news to point to news_detail.htlm. If you don't quite understand what I'm asking, please also read my last question How to get all the post in the same category in Django you so much! here is part of

Make All hashtags clickable in Template with Templatetags

耗尽温柔 提交于 2019-12-02 11:17:35
问题 I want to turn every hashtag in the comment textfield to url so that it will be clickable. For example, a user submit, s = "I can't get enough of #SO because #developers are very #supportive" I want it to return like this in template, I can't get enough of #SO because #developers are very #supportive Where whole text will display and all hashtag will be clickable by embedding {hashtag}. I tried the below templatetags code but it won't return the hashtags with the text. It will only return

Use of “if” in template with custom template tag with multiple arguments

混江龙づ霸主 提交于 2019-12-02 11:14:34
I wrote a custom template tag to query my database and check if the value in the database matches a given string: @register.simple_tag def hs_get_section_answer(questionnaire, app, model, field, comp_value): model = get_model(app, model) modal_instance = model.objects.get(questionnaire=questionnaire) if getattr(modal_instance, field) == comp_value: return True else: return False In my template I can use this tag as follows: {% hs_get_section_answer questionnaire 'abc' 'def' 'ghi' 'jkl' %} The function returns True or False correctly. My problem: I'd like to do something like this: {% if hs_get

Two annotations in a single query

微笑、不失礼 提交于 2019-12-02 11:03:49
I am trying to build a query set to do a count, the query set involves three models. class Owner(models.Model): name = models.CharField(max_length=10, null=False) class Location(models.Model): name = models.CharField(max_length=10, null=False) owner = models.ForeignKey(Owner, on_delete=models.SET_NULL, null=True, blank=True) class Asset(models.Model): name = models.CharField(max_length=10, null=false) owner = models.ForeignKey(Owner, on_delete=models.SET_NULL, null=True, blank=True) location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True, blank=True) I am trying to do a

Django STATIC_URL is empty, images doesn't show

喜夏-厌秋 提交于 2019-12-02 10:59:52
I know that there is plenty of questions like that, I read them all and tried everything, however nothing solved my problem. I'm writing something like blog in Django and of course I want to show pictures to users. Pictures are stored at /static/users/<username>/<image> . My setting.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors

how do I replace no username with a string of choice in Django

删除回忆录丶 提交于 2019-12-02 10:51:18
I would like to have 'No user assigned' when there is no user to the ticket. I did this to my model but I don't see any changes def __unicode__(self): print 'gets here****' if not self.assigned: return 'No user assigned' else: return self.assigned where assigned is the user assigned for a ticket. IS ther any other way to do this? I have my template and view like this: {% for ticket in tickets %} <tr> <td>{{ ticket.title }}</td> <td> {% for user in ticket.assigned.all %} {{ user.email }}{% if not forloop.last %},{% endif %} {% endfor %} </td> and my View: def get_data(self, **kwargs): context =

how to pass the variable from included template to the template where it is included?

六眼飞鱼酱① 提交于 2019-12-02 10:50:31
In Django Views:- if request.is_ajax(): t = get_template('bar-templates.html') html = t.render(Context({'edit': True, 'user':'some-user' })) return HttpResponse(html) There is two templates: Main template (foo-templates.html) which includes the template (bar-templates.html) . In context edit and user is passed to the bar-templates.html But this variable is also used in foo-templates.html . In django we used to {{ edit }} to catch the variable. Since this variable comes in bar-templates.html . How can I use this to foo-templates.html . foo-templates.html: {% extends "base.html" %} <div class=

Django Ajax Form submit wrongly redirect to another page

霸气de小男生 提交于 2019-12-02 10:34:52
When I use ajax to submit a comment form in Django,the page will redirect to a blank page shows me the success data: {"status":"success", "msg":"添加成功"} ,but not stay in current page.I want the page stay in current page and show me the new comment. Here is my update_comment view: def update_comment(request, news_pk): news = get_object_or_404(News, id=news_pk) comment_form = CommentForm(request.POST or None) if request.method == 'POST' and comment_form.is_valid(): if not request.user.is_authenticated: return render(request, 'login.html', {}) comments = comment_form.cleaned_data.get("comment")

Django aggregation in templates?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 10:34:44
I'm thinking a bit about the concept of Django aggregates. I don't quite "get" how they can be used in my case. Basically i have a three-tier hierarchy of objects in my model, and the lowest object (Bar) contain values I want to aggregate. class Bar(models.Model): amount = models.FloatField() class Foo(models.Model): bars = models.ManyToManyField(Bar) class MyUser(models.Model): foos = models.ManyToManyField(Foo) I want my template to do the equivalent of this: {% for user in users %} <h2>{{ user }}</h2> <table> <tr> <td>Foo</td>< <td>Average bar amount</td> </tr> {% for foo in user.foos.all %