django-templates

How to get the name of current app within a template?

本秂侑毒 提交于 2021-01-20 17:51:49
问题 What's the simplest way to access the name of the current app within template code? Alternatively, what's the simplest way to define a template variable to hold the name of the current app? (The goal here is to minimize the number of places I need to edit if I rename an app.) 回答1: There's a way to obtain an app name for a current request. First, in your project's urls.py, considering your app is called 'main' : #urls.py url(r'^', include('main.urls', app_name="main")), Then, a context

NoReverseMatch at / Reverse for 'post_detail' with keyword arguments '{'pk': ''}' not found

孤人 提交于 2021-01-20 13:12:09
问题 I've been trying to get this app to work, I run: python manage.py runserver - & everything was fine, I was able to see the website, login as a superuser, make a comment, however in trying to view the post I started to get an error. Now, if I just go to the site, i get an error. I get this error message: File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with _prefix raise NoReverseMatch(msg) django.urls.exceptions

How best to capture variables from within a for-loop in Django template

江枫思渺然 提交于 2021-01-07 02:48:49
问题 I have two querysets: type and age_group . type queryset: <QuerySet [<Type: Cat>, <Type: Dog>, <Type: Other>]> age_group queryset: <QuerySet [<AgeGroup: Young>, <AgeGroup: Baby>, <AgeGroup: Adult>, <AgeGroup: Senior>]> I loop through these from within my template form so that I can grab the pk when one has been selected, but I cannot capture the variable from within the for loop. How do I capture a variable from within a for loop when using Django? I want to capture pk for type and pk for age

Not able to update an item in Django

大兔子大兔子 提交于 2021-01-07 01:46:13
问题 I am trying to update the Bar Information for users by getting their details and setting the Bar instance. But every time I click on the link on my dashboard it redirects me to the dashboard which is what I used for the else statement if it is not a post method. view.py def UpdateUserBar(request): user = request.user.id bar = Bar.objects.get(user_id=user) form = UpdateBar(instance=bar) if request.method == 'POST': form = UpdateBar(request.POST,request.FILES, instance=bar) if form.is_valid():

Not able to update an item in Django

孤街浪徒 提交于 2021-01-07 01:43:48
问题 I am trying to update the Bar Information for users by getting their details and setting the Bar instance. But every time I click on the link on my dashboard it redirects me to the dashboard which is what I used for the else statement if it is not a post method. view.py def UpdateUserBar(request): user = request.user.id bar = Bar.objects.get(user_id=user) form = UpdateBar(instance=bar) if request.method == 'POST': form = UpdateBar(request.POST,request.FILES, instance=bar) if form.is_valid():

How to iterate over a list in django templates? [duplicate]

我与影子孤独终老i 提交于 2021-01-03 07:29:46
问题 This question already has answers here : Using index from iterated list (2 answers) Closed 5 years ago . I'm passing 4 lists of the same length and the length of the lists to my template. How do I iterate over the lists? views.py def allbooks(request): ID = [1,2] bookName = ["Python", "Java"] author = ["idk", "who"] copies = [3,7] return render(request, 'allbooks.html',{'ID':ID,'bookName':bookName, 'author':author, 'copies':copies,'range':range(len(ID))}) allbooks.html {% extends 'base.html'

python/django: model object has no attribute 'prefetch_related'

青春壹個敷衍的年華 提交于 2021-01-03 06:50:25
问题 I have created a model 'VehicleDetails' in which a user can fill the details of a vehicle and another model 'TripStatus' in which he updates the vehicle location. I wanted to get the latest location for which i did as in my below code. I use prefetch_related in my view to returns the location values for a particular vehicle. But, when after running the server, it raises an error : "TripStatus object has no attribute 'prefetch_related'". I would appreciate helping me solve this. models.py:

Django: TemplateSyntaxError, Invalid filter

僤鯓⒐⒋嵵緔 提交于 2020-12-30 09:51:07
问题 I'm trying to access a value from a dictionary using a variable, all in an HTML file that follows Django's Templating language. Django's templating language doesn't let you access a dictionary using a variable as a key, so I decided to use Django filters to do it. However, I am getting a "Invalid filter" error and I can't figure out why. My html file <table class="table"> <tbody> <tr> <th scope="row">Username</th> <th scope="row">Full Name</th> <th scope="row">Points</th> {% for subject in

In Django, how to display times with lowercase am/pm in templates?

杀马特。学长 韩版系。学妹 提交于 2020-12-29 05:54:19
问题 Django's date/time formats have a code for displaying the am/pm part of times as either: a.m. or AM but not as: am i.e. lowercase without periods. How do you render times with lowercase am/pm? NOTE: I thought of the answer while typing in this question, so I figured instead of scrapping it I'd share my answer in case it's helpful to someone else. 回答1: Use the lower template tag with the time formatting tag, e.g. {{object.time|time:"g:iA"|lower}} renders as 12:30pm compared to {{object.time

Using forloop.counter value as list index in a Django template

こ雲淡風輕ζ 提交于 2020-12-29 05:16:08
问题 in my Django 1.1.1 application I've got a function in the view that returns to his template a range of numbers and a list of lists of items, for example: ... data=[[item1 , item2, item3], [item4, item5, item6], [item7, item8, item9]] return render_to_response('page.html', {'data':data, 'cycle':range(0,len(data)-1]) Inside the template I've got an external for loop, that contains also another for cycle to display in output the contains of the inner lists of data in this way ... {% for page in