I am getting a TemplateSyntaxError whenever I try to run a search using gitpaste:
<a href='{% url owner_pastes owner=result.object.owner.pk %}'>
{{ result.object.owner }}
</a>
The urls.py is laid out as:
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
urlpatterns = patterns('saic.paste.views',
url(r'^live/$', 'live_paste', name='live_paste'),
url(r'^(?P<pk>\d+)/$', redirect_to, {'url': '/paste/%(pk)s/'}),
url(r'^(?P<pk>\d+)/(?P<private_key>[a-zA-Z0-9]+)?/?$', redirect_to, {'url': '/paste/%(pk)s/%(private_key)s/'}),
url(r'^owner/$', redirect_to, {'url': '/owner/all/'}),
url(r'^owner/anonymous/', 'user_pastes', name='anon_pastes'),
url(r'^owner/(?P<owner>.+)/', 'user_pastes', name='user_pastes'),
url(r'^paste/(?P<pk>\d+)/adopt/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_adopt', name='paste_adopt'),
url(r'^paste/(?P<pk>\d+)/embed/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_embed', name='paste_embed'),
url(r'^paste/(?P<pk>\d+)/edit/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_edit', name='paste_edit'),
url(r'^paste/(?P<pk>\d+)/fork/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_fork', name='paste_fork'),
url(r'^paste/(?P<pk>\d+)/favorite/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_favorite', name='paste_favorite'),
url(r'^paste/(?P<pk>\d+)/delete/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_delete', name='paste_delete'),
url(r'^paste/(?P<pk>\d+)/raw/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_raw', name='paste_raw'),
url(r'^commit/(?P<pk>.+)/adopt/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'commit_adopt', name='commit_adopt'),
url(r'^commit/(?P<pk>.+)/download/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'commit_download', name='commit_download'),
url(r'^paste/(?P<pk>\d+)/(?P<private_key>[a-zA-Z0-9]+)?/?$', 'paste_view', name='paste_view'),
url(r'^users/$', 'users', name='users'),
url(r'^favorites/$', 'favorites', name='favorites'),
url(r'^accounts/login/$', 'login', name='login'),
url(r'^accounts/logout/$', 'logout', name='logout'),
url(r'^accounts/register/$', 'register', name='register'),
url(r'^accounts/preference/$', 'preference', name='preference'),
url(r'^accounts/timezone/$', 'set_timezone', name='set_timezone'),
url(r'^$', 'paste', name='paste'),
)
From reading the Django documentation, it seems that I need a url pattern for owner. This is the rest of the error I am getting:
Caught NoReverseMatch while rendering: Reverse for 'owner_pastes' with
arguments '()' and keyword arguments '{'owner': 5}' not found.
It looks like the URL is there. What is wrong?
The name of the URL is not owner_pastes but user_pastes according to the following line:
url(r'^owner/(?P<owner>.+)/', 'user_pastes', name='user_pastes'),
Try this:
<a href='{% url user_pastes owner=result.object.owner.pk %}'>{{ result.object.owner }}</a>
Side-note: be careful, with Django >= 1.5, you need to surround the first parameter by quotes (otherwise it is treated as a variable):
<a href='{% url "user_pastes" owner=result.object.owner.pk %}'>{{ result.object.owner }}</a>
Related
I created a custom User model following the example in the Django documentation, now I'm trying to use Django auth views but I keep getting NoReverseMatch at /accounts/login/
Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This is the url conf:
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html',
'authentication_form': LoginForm}),
url(r'^logout/$', auth_views.logout, {'next_page': '/accounts/login'}),
url(r'^$', home, name="home"),
]
And I have this line in my template:
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% url 'django.contrib.auth.views.login' %}
This is incorrect. We put the name given to the url here instead of the location of the view.
Please see https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url. You need to provide a name for login like you have for home and then use that.
Correct way is:
urls.py ->
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html','authentication_form': LoginForm}, name="login") ,
template ->
<form method="post" action="{% url 'login' %}">
Here is my url.py
from django.conf.urls import url
from django.contrib import admin
from app import views, auth
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name = 'index'),
url(r'^login/', auth.login, name = 'login'),
url(r'^logout/', auth.logout, name = 'logout'),
]
When I'm using in template <li>Administration</li> get error
NoReverseMatch at /
Reverse for 'admin' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
So can any one tell me how to solve this? Thank you very much.
Use admin:index if you want to have an url to /admin/ site.
If you install django-extensions you can use ./manage.py show_urls to get the list of urls for your app
Set admin url in your project urls.py, same folder as your settings.py
Url(r'^admin/', admin.site.urls),
Then call it in your template:
<a href="{% url 'admin:index' %} > link </a>
You should use admin namespace, like written in the docs. You could also look on other admin urls in that namespace.
{% url 'admin:index' %}
I am trying to pass configure a URL like so:
/details/12345
Template HTML:
<div class="row">
{% if article_list %}
{% for article in article_list %}
<div>
<h2>{{ article.title }}</h2>
<p>{{ article.body }}</p>
<p><a class="btn btn-default" href="{% url 'details' article.id %}" role="button">View details ยป</a></p>
</div><!--/.col-xs-6.col-lg-4-->
{% endfor %}
{% endif %}
</div><!--/row-->
urls.py (full):
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'news_readr.views.home', name='home'),
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py:
from django.shortcuts import render
from .models import Article
# Create your views here.
def home(request):
title = "Home"
article_list = Article.objects.all()
for article in article_list:
print(article.id)
context = {
"title": title,
"article_list": article_list,
}
return render(request, "home.html", context)
def details(request, article_id = "1"):
article = Article.objects.get(id=article_id)
return render(request, "details.html", {'article': article})
I am getting an error that says:
NoReverseMatch at /
Reverse for 'details' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/(?P<article_id>\\d+)/$']
I'm one week old at Django, and I think there's something wrong with my URL Named Group config. Please help! TIA!
Update: If I remove the URL config and change it back to:
url(r'^details/$', 'news_readr.views.details', name='details'),
The error changes to:
Reverse for 'details' with arguments '(1,)' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/$']
So it seems to be picking up the argument that is passed 1 in this case. So this seems to be an issue with the regular expression. I tried the expression out at Pythex, but even there, the expression doesn't seem to be matching anything.
For the url pattern
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
The correct way to use the tag is
{% url 'details' article.id %}
This because the details url pattern has a group article_id, so you have to pass this to the tag.
If you have the above url pattern, and {{ article.id}} displays correctly in the template, then the above template tag should not give the error Reverse for 'details' with arguments '()'. That suggests you have not updated the code, or you have not restarted the server after changing code.
If you change the url pattern to
url(r'^details/$', 'news_readr.views.details', name='details')
then you need to remove the article.id from the url tag.
{% url 'details' %}
I guess your pattern is wrong.( not an expert of regex ).
Try this
url(r'^details/((?P<article_id>[0-9]+)/$', 'news_readr.views.details', name='details'),
I'm working through the Django tutorial anf. For some reason, if I try to manually type in the URL like so: http://localhost:8000/polls/1/ I can view the poll (in this case the id of the poll is 1). But if I'm at the index of all the polls, which would be the URL: http://localhost:8000/polls and I try to click on one of the polls, it takes me to an error page with the following:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<pk>\d+)/$ [name='detail']
^polls/ ^(?P<pk>\d+)/results/$ [name='results']
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/{% url 'polls:detail' poll.id % }, didn't match any of these.
Here's my polls/urls file:
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
And my mysite/urls file:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^admin/', include(admin.site.urls)),
)
And my template file:
{% 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 %}
I've seen similar questions but it doesn't seem that they apply to me. Why does it work when I type in the URL but not when I click on the link that should take me to the same place?
You have space between % and }, try this
<a href="{% url 'polls:detail' poll.id %}">
According to the Django docs, a NoReverseMatch happens when "a matching URL in your URLconf cannot be identified based on the parameters supplied."
I am getting the following NoReverseMatch error. My question is: why is the parameter supplied not being caught by the url? Is it expecting a parameter of a different type? I'm still not too comfortable with Django urls.
"Reverse for 'recall' with arguments '(<Unordered_Group: countries>,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
This question is revised from Django NoReverseMatch url issue after suggestions were tried.
edited:
images/urls.py (project level)
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^images/', include('images_app.urls', namespace="images_app")),
url(r'^associate/', include('associate.urls', namespace="associate")),
url(r'^admin/', include(admin.site.urls)),
)
associate/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^learn/', "associate.views.learn", name='learn'),
url(r'^recall/(?P<ordered_group>\w+)', 'associate.views.recall', name='recall'),
url(r'^$', "associate.views.index", name='index'),
)
learn.html
<form action="{% url 'associate:recall' ordered_group %}" method="post"> ERROR CAUGHT
{% csrf_token %}
<div>
<label for="recall">enter as many members of {{ ordered_group }} as you can recall </label>
<input type="text" id="recall" name="recall">
</div>
<div id="enter_button">
<input type="submit" value="enter" name="enter" />
</div>
<div id="done_button">
<input type="submit" value="done" name="done" />
</div>
</form>
views.py
def recall(request, ordered_group):
...
def learn(request):
...
ordered_group = ordered_groups[index]
return render(request, 'associate/learn.html', {'dataset':model, 'ordered_group':ordered_group})
The issue is, your URL pattern is expecting a regex which matches [\w]+ which is one or more wordcharacters.
recall/(?P<ordered_group>\w+)
But it actually got an object.
A better way of doing this would be to send the id of the ordered group object (or any other unique identifier), and querying for that object in the view again.
Note that if you go with the id, URL pattern regex would be
recall/(?P<ordered_group>\d+)
and the view:
def recall(request, ordered_group):
obj = get_object_or_404(Unordered_Group, id=ordered_group)
#rest of the code..