Django: How to check the view that renders a template - python

Want to know how to identify and output the view function that renders a template in HTML.
Need it for debugging purposes.
Something like this:
{% if request.view == "index" %}
<title>Company Name</title>
{% else %}
<title>{{ other_page_title }} ยป Company Name</title>
{% endif %}

You could use:
{% if request.resolver_match.url_name == "index" %}
Comparing against the name given to the url in your urls.py, for example:
urlpatterns = [
path("home/", views.index, name="index"),
]

If you're using class-based views, you could add a name attribute to your view class, and then access it in your template.
class MyView(TemplateView):
name = 'my_view'
You can access that in your template with {{ view.name }}.

Related

Django can't find the URL pattern

I'm new to Django and I'm making wiki website based on markdown. I am having one problem. Django can't match the path in html to urls.py. It happens when I try to open wiki entries. It gives me the following error. I have already passed the parameter to 'page', I honestly have no idea what to do.
Using the URLconf defined in wiki.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
wiki/<str:page> [name='page']
search [name='search']
create [name='create']
addentry [name='add_entry']
nomatches [name='nomatches']
results [name='results']
edit/<str:page> [name='edit']
random [name='random']
The current path, { url 'page' entry }, didn't match any of these.
Please, tell me how can I fix this.
index. html:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<h1>All Pages</h1>
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
{% endblock %}
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:page>", views.viewpage, name="page"),
path("search", views.search, name="search"),
path("create", views.create, name="create"),
path("addentry", views.add_entry, name="add_entry"),
path("nomatches", views.search, name="nomatches"),
path("results", views.search, name="results"),
path("edit/<str:page>", views.edit, name="edit"),
path("random", views.random, name="random")
]
part of views.py:
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def viewpage(request, page):
content = util.get_entry(page)
return render(request, "encyclopedia/page.html", {
"content": markdown2.markdown(content),
"title":page
})
page.html
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
edit
<h1>{{ title }}</h1>
{{ content|safe }}
{% endblock %}
Since the URL for 'page' is 'wiki/<str: page>' you need to define a get_absolute_url method on your class model that contains the object in question, so do add this method in the models.py class that contains page:
def get_absolute_url(self):
return reverse('page',kwargs={str:self.str})
Then, in the template, you can refer to it as
{% object.get_absolute_url%}.

How do I fix a reverse error in Django when redirecting?

I am currently working on a website where you can create a shopping list. I am trying to insert items into the shoplist. So things like banana, cake, etc would go into shoplist. I got everything to work. When I create the item, it goes inside the database but when I try to redirect back to the website where I pressed create item, it shows the error
Reverse for 'detail' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['shoplist/(?P<item_id>[0-9]+)/$']
Also, when I try to enter my details page, it shows the error
Reverse for 'createitem' with arguments '('',)' not found. 1 pattern(s) tried: ['shoplist/(?P<item_id>[0-9]+)/createitem/$']
I think I did something wrong when making my paths or doing something wrong syntax wise. Nothing I found on the internet is fixing it. Is there a way to fix this problem? Thank you very much!
views.py
def createitem(request, item_id):
if request.method == 'GET':
return render(request, 'shoplist/createitem.html', {'form':ItemForm(), 'id':item_id})
else:
form = ItemForm(request.POST)
itemlist = form.save(commit=False)
itemlist.shoplist = Shoplist.objects.filter(user=request.user, pk=item_id).first()
itemlist.user = request.user
itemlist.save()
return redirect('detail', pk=item_id)
urls.py
from django.contrib import admin
from django.urls import path
from shoplist import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
#authentication
path('signup/', views.usersignup, name='usersignup'),
path('logout/', views.userlogout, name='userlogout'),
path('login/', views.userlogin, name='userlogin'),
path('create/', views.createlist, name='createlist'),
path('shoplist/', views.currentshoplist, name='currentshoplist'),
path('shoplist/<int:item_id>/', views.detail, name='detail'),
path('shoplist/<int:item_id>/createitem/', views.createitem, name='createitem'),
]
detail.html
{% extends 'shoplist/base.html' %}
{% block content %}
<h2>{{ error }}</h2>
<h1>{{ shopitems }}</h1>
{% for i in item %}
{{i.item}}
{% endfor %}
<form action="{% url 'createitem' item_id %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create Item</button>
</form>
{% endblock %}
You want to redirect to the 'detail' view and the arg required for it is item_id as an int. I think you want redirect('detail', item_id=item_id). However, you probably want to get the created pk from the form... Maybe form.instance.pk? So redirect('detail', item_id=form.instance.pk). It's not clear if that form is for saving the same object type as you're viewing with the 'detail' view.
For the {% url %}, I don't think you can use anything but keyword args. So, {% url 'createitem' item_id=item_id %} if you put item_id into the template context.

Pass additional context variable data into allauth views using class based views

Not sure how to pass additional context data into various allauth forms which includes my own templates. For my own views I'm using get_context_data() which is working fine. I'm including small templates into a master template such as a header, footer, side bar etc. Everything is working except when allauth kicks in such as login, logout, email confirmation window etc my context variables are not passed so images in my left side bar are not showing up but allauth works fine.
I've tried a few things but I believe the ideal option is to inherit from allauth views for that function such as login, password reset, confirm email etc, supply my own context variable data.
In my accounts.views.py, I'm expecting this to fail as the template doesn't exist but the form still shows up and the UserProfile image isn't being shown in the left side bar.
from allauth.account.views import ConfirmEmailView
class EmailViewExt(ConfirmEmailView):
template_name = "account/signup_alternate1.html"
def get_context_data(self, **kwargs):
context = super(ConfirmEmailView).get_context_data(**kwargs)
context['userprofile'] = UserProfile.objects.get(user=self.request.user)
return context
In my template left_bar_base.html which is included from my overridden allauth template.
{% if userprofile.avatar_picture %}
<img src="{{ userprofile.avatar_picture.url }}" class="card-img-top" alt="...">
{% else %}
<img src="{% static 'placeholder.png' %}" class="card-img-top" alt="...">
{% endif %}
In my email_confirmation.html I have this at the top.
{% extends "ui/base.html" %}
{% load i18n %}
{% load account %}
{% block header %}
{% include "ui/loggedin_header.html" %}
{% endblock %}
{% block left %}
<div class="row no-gutters justify-content-start">
{% include 'ui/left_bar_base.html' %}
{% endblock %}
{% block content %}
... allauth template code...
Came across the solution.
My EmailViewExt(ConfirmEmailView) was never being called.
So instead of using allauths.urls I put this right above the allauths.urls.
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/email/', EmailViewCustom.as_view(), name="email"),
path('accounts/', include('allauth.urls')),
Now the context variable I've added is being passed into my templates. So I guess I have to add all of the urls from allauth for the ones I want to replace.

How to show haystack(whoosh) results into a separate page?

I have a index.html where my form is implemented:
...
{% block searchform%} {% endblock %}
...
In my search/ folder I have two files.
search.html, which is the actual form for the index.html
{% extends 'index.html' %}
{% block searchform %}
<form method="get" action="/results/">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% endblock %}
and the file result.html, where I want to see my search results:
{% if query %}
<h3>Results</h3>
...
{% endif %}
urls.py:
from django.conf.urls import include, patterns, url
from dev_devices.views import *
urlpatterns = patterns('',
url(r'^$', include('haystack.urls')),
url(r'^results/$', results, name = 'results')
)
view.py
def results(request):
return render_to_response('results.html')
My problem is, that there are no results showing up. When I click on the submit Button of my form, I will redirected to the correct file (results.html), but no results are showing up. This question exists already but i found no answer, so can someone help me? Thank you !
Well, you aren't passing any results to the template. Check out your views.py:
def results(request):
return render_to_response('results.html')
Your template has this logic:
{% if query %}
<h3>Results</h3>
...
{% endif %}
query is not a variable in context. You'd have to pass the query in from the view:
def results(request):
return render_to_response('results.html', {'query': 'some query'})
But more than that, you need to get the actual results from Haystack and put those in context too. I think you are confused about what's happening here. If you want to use haystacks default views for search/results, then you shouldn't define any custom views at all. The way your results view is defined, how is Django to know this page has anything to do with Haystack at all?
If you do want to define custom views, then those views need to implement Haystack search forms, creation of SearchQuerySets, pagination, (and so on), and pass that data to the template. See the Haystack documentation on creating you own views:
http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#creating-your-own-view
If you just want to see your results and this particular URL structure is not important to you, then just change your form action to point at the same page action="" and move your results display logic from results.html to search.html. That should get you the basic search results that ship with Haystacks default views and forms.

Django template multiple url return

I've 2 urls pointing to the same place:
url(r'^index/$', 'proj.views.index'),
url(r'^index/show_closed/$', 'proj.views.index'),
If I use in a template file this:
{% url proj.views.index %}
it' ll return /index/show_closed
and this is OK, but how can i make {% url %} to return only /index ?
Use named patterns; giving your pattern a unique name:
url(r'^index/$', 'proj.views.index', name='index_direct'),
url(r'^index/show_closed/$', 'proj.views.index', name='index_closed'),
then refer to those names in the {% url %} tag instead of the view:
{% url 'index_direct' %}
{% url 'index_closed' %}

Categories