Questions tagged [django-templates]

1

votes
3

answer
1.7k

Views

Django: Get the form name or id from template

If you have multiple forms from django import forms class NameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) class SecondNameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100) is there a way that, in your template, you can figure out i...
Algorithmatic
0

votes
0

answer
25

Views

Showing display values in form built with a loop

I have a simple model form for adding new objects. The template builds the form with a loop, i.e not every field is placed explicitely. The model has a few choice fields. Normally, if you want to see the the display value of the choice field you'd do something like {{ get_myfieldname_display }}. bu...
uwain12345
1

votes
1

answer
286

Views

Django. Pass an array to a child template via include tag

I'm trying to create an array on the fly and pass it to a child template: {% include "template.html" with foo = ['one', 'two'] %} but this doesn't work. Does anybody know how to generate an array in parent template and then pass it to another template?
MrBinWin
1

votes
1

answer
956

Views

Different css margins for mobile & desktop without multiple {% block content %} in Django template

I need to change the margin-left that is applied to my main {% block content %} in my base.html template (that contains my navbar and other common elements) based on if the viewer is using mobile or desktop. My current base.html is like: {% block content %} {% endblock %} with a css file containing:...
TimJ
0

votes
0

answer
8

Views

I can't access a Django template variable in JS script

I'm having trouble accessing the list param = ['foo','bar'] on myView.js. the JS code is treating param as if it were a string, but when I try JSON.parse(param) I get the error shown below. What am I missing here? myView.html: window.obj = {}; obj.param = "{{ param | safe }}"; views.py: def myView(r...
Jane
0

votes
0

answer
24

Views

static files in django is not working (404) although the path is right

the html page appear without any style or images because of not finding the static files in Django the load : {% load staticfiles %} this is the static call : the url setup : STATIC_URL = os.path.join(BASE_DIR, "static/")
Abanoub Magdy
1

votes
2

answer
7.5k

Views

Django - getting Error “Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:” when using {% url “music:fav” %}

I am learning django framework from last 4 days. Today I was trying to retrieve a URL in HTML template by using {% url "music:fav" %} where I set the namespace in music/urls.py as app_name= "music" and also I have a function named fav(). Here is the codes: music/urls.py from django.urls import pat...
Vishal Ghosh
0

votes
0

answer
3

Views

How To Download HTML Data As Doc File From Django DetailView

I am trying to figure out if there is a way to natively download data from my Django DetailView. I have been reading SO and getting ideas for the last day or so and I almost have it working.... Here is my code... class AuthorDetailView(LoginRequiredMixin,DetailView): model = Author context_object_n...
Steve Smith
1

votes
1

answer
197

Views

django - showing OrderedDict in templatetag

I have a OrderedDict and I need to show its key, value but I am unable to get its value, I have this dict = ([('sainbath', 'thesailor'), ('HELLO', 'WORLD')]), I have tried this {{object.specifications}} {% for key in object.specifications %} {{key}} {%endfor%} I am getting this output OrderedDict([(...
Pankaj Sharma
1

votes
2

answer
427

Views

Django: How to nicely format an address in a template without knowing how much information is available?

I have a table with address information. It has these fields. address_1 address_2 city state zip No field is required. I want to display this in a template and format it nicely. I want line breaks between address_1, address_2, and the city/state/zip line for any of those that exists. I also want a c...
raddevon
1

votes
1

answer
3.9k

Views

display django model data in custom template

I have django models such as this class District(models.Model): name = models.CharField(max_length=30,unique=True) number = models.PositiveIntegerField(null=True,blank=True) def __unicode__(self): return "District (%s,%s)" % (self.name,self.number) class Meta: db_table = 'districts' And i can be ab...
roykasa
1

votes
1

answer
3.4k

Views

UpdateView is missing a QuerySet Error

As of today, my UpdateView no longer works. Whenever I select the icon to edit an item I get the following error: EntryUpdate is missing a QuerySet. Define EntryUpdate.model, EntryUpdate.queryset, or override EntryUpdate.get_queryset(). I have never had to have a QuerySet within my UpdateView befor...
grigs
1

votes
1

answer
304

Views

Translating dynamic content in django templates

I have a html template rendered by a view in django. And the template has some dynamic values that the view sends. Example:: {{ text_to_be_translated.brand_name}} The above 'text_to_be_translated.brand_name' is a dictionary with thousands of keys like brand_name, which can hold many values like 'my...
1

votes
2

answer
341

Views

Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\\/(?P<news_pk>[0-9]+)$']

I'm coding a news site.Now I'm detailing with the comment post function.And meet the issue says: Reverse for 'update_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comment\\/(?P[0-9]+)$'] I have tried many ways and times but still can't solve it and I can't find any wrong with my c...
William
1

votes
2

answer
1.2k

Views

Django - print all objects attribute values

HTML {% for field in fields %} {{ field }} {% endfor %} {% for well in well_info %} {{ well.api }} {{ well.well_name }} {{ well.status }} {{ well.phase }} {{ well.region }} {{ well.start_date }} {{ well.last_updates }} {% endfor %} views.py class WellList_ListView(ListView): template_name = 'well_li...
Eric Kim
1

votes
1

answer
641

Views

Django redirect - not a valid view function or pattern name error

I'm a newbie in Django and am trying to set up a simple contact form which redirects to a thank you page on successful submission. I'm having trouble getting it to redirect to the thanks page after submission and getting the below error: NoReverseMatch at /contact/ Reverse for 'thanks' not found. '...
locke14
1

votes
2

answer
50

Views

how can i display other user information in django

I'm creating a website that the user can look at other users profile but the problem is when the user enter another user profile it show his personal information this is the urls.py file code urlpatterns = [ path('user/', UserPostListView.as_view(), name='user-posts'), ] this is the view.py file c...
Gehad Al Sayed
1

votes
2

answer
2.1k

Views

@login_required decorator in django 1.1 and Template_name

I uses @login_required in my views.. and in the django book they tell i you don't want to use the default registration/login.html template you can pass en extra view argument template_name. But i v tried : @login_required(template_name='washtest/login.html') but i have an error login_required() got...
laurent
1

votes
1

answer
187

Views

django degree sign

I'm trying to put a degree sign into a string that gets passed to a template. I've tried ° which of course got escaped. Then I tried using \u00b0 and \xb0 like so: def __str__(self): return u"%s (%s \xb0C to %s \xb0C)" % \ (self.name, self.min_deg_c, self.max_deg_c) Both of these have resulted in:...
CrazyCasta
1

votes
5

answer
7.2k

Views

bad operand type for abs(): 'str' in django

I am going through the beginning django ecommerce application for shopping cart app. I am getting the error as mentioned above while clicking the add to cart button. I am getting the error at the line {{ cart_sutotal|currency}}
user1909601
1

votes
2

answer
1.7k

Views

Rendering custom template tags from database in Django

I am pretty new to Django and am trying to build a blog app for my website. I have a model I have created to store blog posts that includes a text field for the post body, created and converted to HTML using TinyMCE (via Grappelli). I would like embed custom template tags within this post body tha...
woemler
1

votes
1

answer
2.4k

Views

django template for loop some table rows display only once

Model: class Subject (models.Model): name = models.CharField(max_length=50) day = models.CharField(max_length=50) In my sql table i have more than 50 users and everyone have assigned one day of a week (ie. user1 -> MON, user2 -> MON, user3 -> WED ...) In template below I want to display table with D...
Chris
1

votes
2

answer
1.7k

Views

Reference a url with an id by name in Django

I want to reference a dynamic url in my templates using its name, but am not sure how to incorporate the object id. In other words, I want to reference "/products/98" in my template without having to hard code it (as my url patterns might change). In my urls.py, I have: url(r'^products/(\d+)/$', '...
Justin
1

votes
1

answer
1.5k

Views

Django CSRF verification failed on post method

I dont understand this one... the token is in the template and the 'django.middleware.csrf.CsrfViewMiddleware' is define in MIDDLEWARE_CLASSES views.py: from django.shortcuts import render_to_response from ezmapping.models import * from django.forms.models import modelformset_factory def setMapOpti...
Below the Radar
1

votes
3

answer
237

Views

django: mysterious padding of strings in template

I'm using the following template code to try to produce a list in the format 'a, b, c and d': {% for item in list %} {% if not forloop.first %} {% if not forloop.last %} , {% endif %} {% endif %} {% if forloop.last %} and {% endif %} {{ item }} {% endfor %} The actual output I'm getting is 'a , b ,...
David
1

votes
1

answer
635

Views

Django template roundup scientific notation

I'm trying to convert, eg: 0.00282357936911 to 2.82e-03 and 0.000130076846614 to 1.30e-04 However, when I use the filter {{ data|stringformat:"e" }}, I get 2.823579e-03 and 1.300768e-04 Is there are way to roundup to 2 decimal points? Many thanks in advance.
DGT
1

votes
2

answer
4.1k

Views

django template block overriding

I'm developing an application in django and I don't know how to use the Django template system to achieve block overriding using template inheritance. I've got a base.html template: [...] {% block content %} {% endblock %} [...] there's index.html template that displays a JS slider (it's content res...
ducin
1

votes
1

answer
3.4k

Views

Django dynamic menu in global view (base.html) to be visible in all templates

I have built the website with couple templates but I would like to achieve how to read menu from DB in the base.html that would be visible on entire website, I dont want to add it to every template. I found in the docs by POLL example: https://docs.djangoproject.com/en/dev/howto/custom-template-tags...
Radek
1

votes
1

answer
420

Views

Using Django template tag “with” with the result of another template tag

I have a comment_form.html template, which is used in multiple places in my app, and I'd like to be able to pass the endpoint's url into that template from a parent template. Normally I would do this using the with tag: {% with endpoint='/comments' %} {% include 'comment_form.html' %} {% endwith %}...
Andrew Gorcester
1

votes
1

answer
159

Views

How do I reference a template from a class method in Django?

Is it possible to reference a template from a class method in Django? Let's say I have the following class in my models (for an offline speaker series): class Event(models.Model): name = models.CharField(max_length=300) date = models.DateTimeField() desc = models.TextField(blank=True, null=True) lo...
Joe
1

votes
1

answer
1.7k

Views

Django: IndexError at /customerList/ tuple index out of range

Can anyone help me with this. I don't understand this error IndexError at /customerList/ tuple index out of range and it came from this code self.Customer = get_object_or_404(customer, name__iexact=self.args[0]) I want to be able to do ListView some of the fields from two forms that is from customer...
noobes
1

votes
2

answer
701

Views

django-userena removing mugshot

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 t...
MYNE
1

votes
1

answer
1.6k

Views

Django templates iterate through list of tuples and give them all buttons

I have a User model and the user's have university attribute. I want users to be able to scroll through a list of universities and choose one as a button, I don't want to have a dropdown selecter thing. So I passed in the UserInfo.UNIVERSITY_CHOICES (UserInfo is a one to one with the user model) to...
Chase Roberts
1

votes
2

answer
2.8k

Views

Django - Create table from list of list

I am not very good with table in html, so this question might be very simple to answer. I pass the list of list {{ attributes }} to the template and I want to create a table with 2 rows and many columns. TEMPLATE: {% for attr in attributes %} {{ attr.0 }} {{ attr.1 }} {% endfor %} I want the {{ attr...
Below the Radar
1

votes
1

answer
926

Views

How to use one view on two different pages in Django?

I'm building a site with Django 1.5.1. I have Album and Category models defined: ###models.py class Category(models.Model): title = models.CharField(max_length=200, unique=True) class Album(models.Model): category = models.ForeignKey(Category, related_name='albums') I've generated a menu to list aut...
sheshkovsky
1

votes
1

answer
2.5k

Views

Caught VariableDoesNotExist while rendering: Failed lookup for key [time_filter] in u'[{},

views.py def when(request): user = request.user report = Report.objects.get(user=request.user) reportform = ReportForm(instance=report) settings = Settings.objects.get(user=request.user) settingsForm = SettingsForm(instance=settings) settings=Settings.objects.get(user=2) if settings.date_format...
1

votes
1

answer
644

Views

Custom template tag - return objects

I have a base.html which is inherited by all other pages. and the footer of all pages are also coming from base.html now i want to show in footer some objects from db. so i need to render some objects from db to base.html. then i thought, i will write my own tag which does the job. i did this cust...
doniyor
1

votes
3

answer
954

Views

Parse JSON with colon in key

I am trying to parse the following JSON using Python / Django: [ { "street": "KEELER", ":id": 1421 } ] Within my Django templates, I can successfully access the street key like: {{ obj.street }} but cannot access the id. I have tried the following (all taken from various SO questions): {{ obj.id }}...
Progger
1

votes
1

answer
453

Views

django regroup templatetag does not group properly

I have following template {% regroup product.hotel.facilities.all by facilitytype as facilities %} {% for facility in facilities %} {{ facility.grouper }} {% for i in facility.list %} {{ i }} {% endfor %} {% endfor %} And following model structure: class Hotel(TranslatableModel): code = models.Cha...
tunaktunak
1

votes
1

answer
2k

Views

Error retrieving multiple select option in Django

I have a drop down with multiple select option in my html page. On form submission, I am trying to capture all of the selected options by user in that drop down. but it throws me an error instead "TypeError:'instancemethod' object is not subscriptable". Following is my template.html and views.py Tem...
gauravhalbe

View additional questions