I tried to learn django a few day ago and make a simple website use that, but for a reason my dropdown in home page didn't work but in another page it's work properly.
Here my html template for that
<div class="dropdown-menu">
{% for category in links %}
<a class="dropdown-item" href="{{ category.get_url }}">{{category.category_name}}</a>
{% endfor %}
</div>
and here my code for django urls.py
urlpatterns = [
path('', views.store, name='store'),
path('<slug:category_slug>/', views.store, name='product_by_category'),
]
and here my code for links references
def menu_links(request):
links = Category.objects.all()
return dict(links = links)
i don't know why in my home page the dropdown button didn't work but in another page it work. i tried to find on my navbar templates and i think there is no problem there but the dropdown still not working and i tried to find on settings.py(to check installation app) but i did it
views.py
def home(request):
products = Product.objects.all().filter(is_available = True)
context = {
'products' : products,
}
return render(request, 'home.html', context)
anyone have idea where the problem is?
The reason why my dropdown not work in some page is because i'm include header tag in my home.html, even it's already in my base.html template
If someone have similar problem please check your page maybe you make some mistake like me
Related
I am working on a blog website and have set up dynamic URLs in Django for each blog article. Is it possible to change the layout of paragraphs and images of specific pages so that there are slight differences between each page?
Yes it is possible to have different layout for the same template rendered in Django. You can pass some variable in context to achieve this goal like this :
views.py
def detail(request, id):
object = Model.objects.get(pk=id)
context = {'layout': f"layout_for_{id}"}
return render(request, 'template.html', context)
template.html
{% if layout == 'layout_for_2' %}
Layout for 2 here
{% endif %}
You can add more condition as you want, but it can become hard to for too much {% if %} block.
I'm running a program to create a wikipedia style website using python and the django framework. I'm running into an issue where I have a link to edit the wiki page on the entry pages. When the page tries to render I get the error message in the title of this post. I'm not sure why it's not finding the edit page, I think I might have a typo somewhere but I'm not finding it. Any help here would be greatly appreciated!
URLS.py
urlpatterns = [
path("", views.index, name="index"),
path("error", views.error, name="error"),
path("newPage", views.newPage, name="newPage"),
path("random", views.random, name="random"),
path("edit", views.edit, name="edit"),
path("<str:entry>", views.markdown, name="entry")
]
entry.html
{% block nav %}
<div>
Edit Page
</div>
{% endblock %}
views.py entry function
def edit(request, title):
content = util.get_entry(title)
return render(request, "encyclopedia/newPage.html", {
"title": title,
"content": content
})
Your edit URL needs a parameter to specify the entry in the URL, so:
# ↓ url parameter
path('edit/<str:title>/', views.edit, name='edit'),
If the title can contain a slash, you need to use the <path:…> path converter [Django-doc]:
# ↓ url parameter
path('edit/<path:title>/', views.edit, name='edit'),
Hi guys i am new to django...i been watching youtube videos and reading books on Django but am still struggling with templates. I am working on a ecommerce project and i would love a bit of help with templates. So i want my template to display a list of categories as links on a sidebar. I have defined a slug field in my category models and i have managed to map a url...but i am still not getting a list of categories on my index page sidebar.
This is my url pattern and this is working perfect. When i click 127.0.0.1.000/food it's working (food is a category)
path('<slug:category_slug>/', views.category, name='category'),
the view function
def category(request, category_slug):
"""Defines category views"""
categories= get_object_or_404(Category, slug= category_slug)
context = {'categories': categories}
return render(request, "categories_list.html", context)
This is the categories_list.html template that i need help with
<h3> Shop by Category </h3>
{% if category in categories %}
<li>
{{category.name}}
</li>
{% endif %}
My wish is to have the categories displayed on the sidebar of my index page as links. I have used {% include 'category_list.html' %} on my index page template, and its only displaying the Shop by Category heading instead of the categories when i am on the index page. I have tried the for loop in my template but if didn't work, it kept on saying category object not iterable...so i ended up using the if statement. Any help will be appreciated
get_object_or_404() method returns single instance or None if it is not found.
You can't iterate that way on object.
Try next
View
def category(request, slug):
"""Defines category views."""
return render(request, 'categories_list.html', {'category': get_object_or_404(Category, slug=slug)})
Template
{{ category.slug }}
Overview / Problem:
Hi! The wrong template and view are loading every time I click the check_tier_level link in my template.
When that parameter is in, it loads the home view with "check_tier_level" as the special_message, even though my links go to the view for check_tier_level. If I click any of the form buttons to grant access, the proper message shows up in that spot. I just can't check the level.
The app works fine and renders the right template / view only when I remove the special_message parameter from the urlpattern and view.
The only other lead I have on this is that the url in the browser will also look like http://127.0.0.1:8000/tiered_access_app/Tier 1 granted!/, instead of having the characters escaped with %20 and so on.
My goal
The whole reason I want to keep that parameter in is so a special_message can notify users of the latest update based on their actions. If anyone knows a better way to do this without making a whole new view / template (which I know is a solution, and how to do it), I'd like to know how. Anyways, here's my code:
urlpatterns.py
path('', views.home, name='home'),
path('<str:special_message>/', views.home, name='home_special_message'),
path('check_tier_level/', views.check_tier_level, name='check_tier_level'),
path('check_tier_level/gain_access/', views.gain_access, name='gain_access'),
views.py
def home(request, special_message=None):
return render(request, 'tiered_access_app/home.html', {'special_message': special_message})
def check_tier_level(request):
current_user = request.user
try:
find_user = TieredAppCustomUser.objects.get(user=current_user)
if find_user.tier_choice == 'tier1':
return render(request, 'tiered_access_app/check_tier_level.html', {'level_1': 'You have access to level 1.'})
# and so on with other levels...
except ObjectDoesNotExist:
pass
return render(request, 'tiered_access_app/check_tier_level.html', {'no_access': 'You don\'t have access to the content here yet.'})
home.html
{% if special_message %}
<h2>{{ special_message }}</h2>
{% endif %}
<form action="{% url 'tiered_access_app:gain_access' %}" method="POST">
{% csrf_token %}
<label>Check level 1 access</label>
<!-- *******PROBLEM WITH LINK HERE******** -->
<p>Try to access level 1 first. You won't be allowed unless you gain access first, by clicking the button below.</p>
<!-- *******PROBLEM WITH LINK HERE******** -->
<input type="hidden" value='1' name="tier_level">
<input type="submit" value="Enable level 1">
</form>
I FIGURED IT OUT:
All I had to do was change my url patterns into the following order:
path('', views.home, name='home'),
path('check_tier_level/', views.check_tier_level, name='check_tier_level'),
path('check_tier_level/gain_access/', views.gain_access, name='gain_access'),
path('<str:special_message>/', views.home, name='home_special_message'),
The only difference here and what I have below, is the position of the 2nd function that goes to home. I'm going to leave this question up in case someone else comes across this same problem. I don't know why this made it work, but it did. Now everything works perfectly.
I've been trying to figure this out for a while now but I feel like I don't know the framework well enough to debug this myself.
Basically I'm creating a little blog style site and I'm trying to create a list of posts which can link to the page to read the post itself.
I have a for loop in my template:
templates/home.py
<h1>Home Page</h1>
<p>welcome to the ven home page, {{ username }}!</p>
Click here to log out
<br>
Click here to create a post
<h2>Posts:</h2>
{% for post in posts %}
<div>
<hr>
<h4>{{post.title}}</h4>
<p>{{post.body}}</p>
<p><i>{{post.tags}}</i></p>
</div>
{% endfor%}
It's the line <h4>{{post.title}}</h4> which is causing the problem. I'm getting the error
Reverse for 'show' with keyword arguments '{'id': 1}' not found. 1
pattern(s) tried: ['posts/(?P<post_id>\\d+)/view/$']
here is my urls file
url(r'^$', views.CreateFormView.as_view(), name='create'),
url(r'^(?P<post_id>\d+)/view/$', views.show_post, name='show')
The create method link works fine
and here is the view which loads the template:
def home(request):
if not request.user.is_authenticated:
return redirect('users:login')
posts = Post.objects.all()
username = request.user.username
return render(request, 'ven/home.html', {'username': username, 'posts':
posts})
If more information is needed then let me know and I will provide it.
All other answers have said that this error is to do with the namespace, but it's working fine with the create link so I'm stumped.
Thanks in advance!
The argument names are mismatching.
You'd want to change <h4>{{post.title}}</h4>
to
<h4>{{post.title}}</h4>
Since in urls.py the show url is defined as '^(?P<post_id>\d+)/view/$'.