I'm working on simple a pet store app
I encountered this error a couple of times and I managed to understand it and fix it but in this situation . I don't know what went wrong . Everything seem clear to me .
It says I have an error in my store.html at the location of the {% url world:brazil animal.id %} but I have define the namespace already.
Reverse for 'brazil' with arguments '('',)' and keyword arguments '{}' not found.
Error during template rendering
In template C:\djcode\mysite\pet\templates\store.html, error at line 5
Reverse for 'brazil' with arguments '('',)' and keyword arguments '{}' not found.
1 Sydney's Pet Store
2 {% if store %}
3 <ul>
4 {% for a in store %}
5 <li><a href ="{% url world:brazil animal.id %}">{{ a.name }}</li>
6 {% endfor %}
7
8 </ul>
9 {% endif %}
My store.html
Sydney's Pet Store
{% if store %}
<ul>
{% for a in store %}
<li><a href ="{% url world:brazil animal.id %}">{{ a.name }}</li>
{% endfor %}
</ul>
{% endif %}
My views.py
from pet.models import Store , Pet
from django.shortcuts import render_to_response ,get_object_or_404
def index(request):
store = Store.objects.all()
return render_to_response ('store.html',{'store':store})
def brazil(request , animal_id):
store = get_object_or_404(Store , Pet, pk=animal_id)
return render_to_response ('animal.html',{'store':store})
My pet app URLCONF:
from django.conf.urls import patterns,include , url
urlpatterns = patterns ('pet.views',
url(r'^$','index',name = 'index'),
url(r'^(?P<poll_id>\d+)/$','brazil',name ='brazil'),
)
my main URCONF:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = patterns('',
url(r'^pet/',include('pet.urls' , namespace='world' )),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
Because you didn't define animal.id. What you have render in your view is only store variable.
Sydney's Pet Store
{% if store %}
<ul>
{% for a in store %}
<li><a href ="{% url world:brazil a.id %}">{{ a.name }}</li>
{% endfor %}
</ul>
{% endif %}
Related
I need to redirect to another page with same url parameters so I don't understand how to use url parameters in current template to redirect.
dborg.html:
{% extends './frame.html' %}
{% block content %}
Создать нового пользователя
{% csrf_token %}
{{form}}
{% for i in o %}
<p><a href='{% url "view_user" i.id %}'>{{i.fld}}</a></p>
{% endfor %}
{% endblock %}
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.main, name='main'),
path('view_org/<str:orgtype>/<str:deg>',
views.view_org_db, name='view_org'),
path('token/<str:orgtype>/<str:deg>', views.get_token, name='token'),
path('token/<str:orgtype>', views.get_token, name='token'),
path('create/<str:orgtype>/<str:deg>',
views.create_user, name='create_user'),
path('search/', views.search_user, name='search_user'),
path('report/', views.view_report, name='view_report'),
path('view/<int:id>', views.view_user, name='view_user')
]
I have problems to link my custom button from change_form.html to my view function.
pic : admin change form custom button
change_form.html
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block title %} Send Email {% endblock %}
{% block content %}
{% if request.resolver_match.url_name == 'requests_requests_change' %}
<div class="submit-row">
{% csrf_token %}
<a href="{% url 'requests:send-email' original.pk %}"
class="button" style="background-color: #F08000;float: left">Request Subscription
</a>
</div>
{% endif %}
{{ block.super }}
{% endblock %}
views.py
from django.shortcuts import render, redirect
def send_email(request, requests_id):
return redirect('') # or whatever to test the url
urls.py
from django.urls import path
from . import views
app_name = 'requests'
urlpatterns = [
path('<int:pk>/send-email/', views.send_email, name='send-email'),
]
main project urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', admin.site.urls), # admin site administration
path('requests/', include('requests.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
pic: error message
Any help would be great! Thanks!
I have found the solution on my own.
The issue was the url inside the template.
Worked with this one:
Template :
<a href="{% url 'admin:requests_requests_change' original.pk %}send-email/"
And the URL path:
path('<int:pk>/change/send-email/', views.send_email, name='send-email')
I am getting an error saying:
NoReverseMatch at /books/
Reverse for 'urlvar' not found. 'urlvar' is not a valid
view function or pattern name.
I guess the {% with %} tag is not working well in books/index.html
but I don't know how to solve this.
this is my code:
books/urls.py
from django.conf.urls import url
from books import views
urlpatterns = [
url(r'^$', views.BooksModelView.as_view(), name='index'),
url(r'^book/$', views.BookList.as_view(), name='book_list'),
url(r'^author/$', views.AuthorList.as_view(), name='author_list'),
url(r'^publisher/$', views.PublisherList.as_view(), name='publisher_list'),
url(r'^book/(?P<pk>\d+)/$', views.BookDetail.as_view(), name='book_detail'),
url(r'^author/(?P<pk>\d+)/$', views.AuthorDetail.as_view(), name='author_detail'),
url(r'^publisher/(?P<pk>\d+)/$', views.PublisherDetail.as_view(), name='publisher_detail'),
]
templates/books/index.html
{% extends 'base_books.html' %}
{% block content %}
<h2>Books Management Systemt</h2>
<ul>
{% for modelname in object_list %}
{% with 'books:'|add:modelname|lower|add:'_list' as urlvar %}
<li>{{ modelname }}</li>
{% endwith %}
{% endfor %}
</ul>
{% endblock %}
urlvar is a variable, so you use urlvar in the {% url %} tag instead of the string 'urlvar'.
<li>{{ modelname }}</li>
You can construct the URL pattern name in the tag if you prefer, or keep the with tag if you find that more readable.
<li>{{ modelname }}</li>
Since you are using the 'books: namespace when you reverse the urls, you should set app_name in your books/urls.py file.
from django.conf.urls import url
from books import views
app_name = 'books'
urlpatterns = [
...
]
My question: why i am not able to view search only ayan rand's book with classbased list view?
this is my function based view for store list, and i am retrieving all my book objects and rendering in HTML and it is working fine.
But using classbasedview "SearchBookDetail" i am not able to get the specified book details as denoted .
Views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse,HttpResponseRedirect
from django.views.generic import TemplateView,ListView,DetailView
def store_listView(request,):
queryset=Book.objects.all()
context={
"objects_list":queryset
}
return render(request,'bookstores/store.html',context)
class SearchBookDetail(ListView):
template_name = "bookstores/store.html"
queryset = Book.objects.filter(author__icontains='Ayan Rand')
print("Ayan Rand query set", queryset)
Urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.views.generic import TemplateView
from store.views import (Home,ContactView,LoginView,
store_listView,
SearchBookDetail,
book_createview,
QuoteslistView,
AyanRandBookDetail,
quotesFunctionView)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',Home.as_view()),
url(r'^contact/$',ContactView.as_view()),
url(r'^login/$',LoginView.as_view()),
url(r'^store/$',store_listView),
url(r'^store/AyanRandBookDetail/$',AyanRandBookDetail.as_view()),
url(r'^store/SearchBookDetail/$',SearchBookDetail.as_view()),
url(r'^quotes/$',quotesFunctionView)]
store.html:
{% extends "home.html" %}
{% block head_title %}Store || {{ block.super }} {% endblock head_title %}
{% block content %}
<head>
<meta charset="UTF-8">
<title>Store</title>
</head>
<h6>Books available</h6>
<ul>
{% for obj in objects_list %}
<li>{{obj}}<br>
{{obj.book_image}} <br>
{{obj.description}} <br>
{{obj.author}}<br>
{{obj.genre}}<br>
{{obj.price}}<br>
</li>
{% endfor %}
</ul>
{% endblock content %}
ListView sends its data to the template as object_list, not objects_list.
I designed this poll app and i'm trying convert a hard code url into an namespace url but had errors along the path.
This is my index.html and as you can see their an hard coded url that pointing to my URLconf.
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
My myapp URLconf.
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns, include, url
urlpatterns = patterns('myapp.views',
url(r'^$', 'index', name="index"),
url(r'^(?P<poll_id>\d+)/$', 'detail',name="detail"),
url(r'^(?P<poll_id>\d+)/results/$', 'results', name="results"),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote', name="vote"),
)
This is my main URLconf.
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('myapp.urls', namespace='myapp')),
,
)
My views are :
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('myapp/detail.html', {'poll': p},
context_instance=RequestContext(request))
I tried to replace the hard coded error with {% url detail poll.id %} or {% url myapp:detail poll.id %}
but i received this error
NoReverseMatch at /polls/
Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.4.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.
Error during template rendering
In template C:\djcode\mysite\myapp\templates\myapp\index.html, error at line 4
Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.
1 {% if latest_poll_list %}
2 <ul>
3 {% for poll in latest_poll_list %}
4 <li>{{ poll.question }}</li>
5 {% endfor %}
6 </ul>
7 {% else %}
8 <p>No polls are available.</p>
9 {% endif %}
How can I convert this hardcoded URL into an namespace so It could point to myapp URLconf without any errors?
The way of doing it is different depending on use-case.
You can either do, which probably is your usecase.
{% url detail poll.id %}
Where the url-tag matches on the name detail and the following poll.id
The other way we would like to discuss here is if you're going to have multiple instances of the same app then you would have to use url namespaces. Discussed here
Main urls.py
url(r'^polls/', include('core.urls')),
Included urls.py
urlpatterns = patterns('',
url(r'^/(?P<polls_id>\d+)/$', 'core.views.polls_detail_view',name="poll_detail")
)
Template
To Poll Detail
`views.py``
def polls_detail_view(request, poll_detail):
print "Hello Poll detail: %s" % (poll_detail)
** EDIT **
After googling around these two SO posts explains why OP's configuration is a no go.
First SO Post
Second SO Post
These are tickets from 19 months ago.
** EDIT **