I am making a Django project with multiple apps and therefore multiple urls.py files. I am trying to an app for user accounts into a project with apps for the shop, cart, and orders. Specifically, I want to link the account/ pages back to the shop
Main urls.py:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('account.urls')),
url(r'^cart/', include('cart.urls', namespace='cart')),
url(r'^orders/', include('orders.urls', namespace='orders')),
url(r'^', include('shop.urls', namespace='shop')),
]
Urls.py for account/:
urlpatterns = [
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login',name='logout_then_login'),
url(r'^register/$', views.register, name='register'),
url(r'^$', views.dashboard, name='dashboard'),
]
Here is the template I am using for the account page
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/base.css" %}" rel="stylesheet">
</head>
<body>
<div id="header">
<span class="logo">Rachel's Stuff</span>
{% if request.user.is_authenticated %}
<ul class="menu">
<li {% if section == "dashboard" %}class="selected"{% endif %}>
My dashboard
</li>
<li {% if section == "images" %}class="selected"{% endif %}>
Home
</li>
<li {% if section == "people" %}class="selected"{% endif %}>
People
</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }},
Logout
{% else %}
Log-in
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Here, I want to link from 127.0.0.1:8000/account/ back to http://127.0.0.1:8000, which defaults to the main storefront:
<li {% if section == "images" %}class="selected"{% endif %}>
Home
</li>
But I get an error:
Reverse for 'shop' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://127.0.0.1:8000/account/
Django Version: 1.8.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'shop' with arguments '()' and keyword arguments '{}' not >found. 0 pattern(s) tried: []
How can I link back to the main shop page (127.0.0.1:8000/) when I'm already in the account namespace? Sorry if I used any terms wrong.
You are using wrong url name (shop) to reverse. Have a look at shop/urls.py file and see the actual name of the ^$ path. As there is already a namespace defined it should be reversed as shop:<your url name here>.
Related
I have tried everything i found on the internet which was similar to my problem, but it did not help. Please help if you know the answer.
I get an ERROR while trying to set up my post_detail page of my blog.
The ERROR MESSAGE:
Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found.
1 pattern(s) tried: ['post/<int:pk>/']
My post_list.html
{% extends "blog/base.html" %}
{% load static %}
<html>
<head>
<title>Code Reminder</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
</head>
<body>
<div class="page-header">
<h1>Coder Reminder</h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-4">
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
My post_detail.html
{% extends "blog/base.html" %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<h2>{{ post.title }}</h2>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endblock %}
My base.html
{% load static %}
<html>
<head>
<title>Code Reminder</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
</head>
<body>
<div class="page-header">
<h1>Code Reminder</h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
my views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Create your views here.
from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404
def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, '/home/ud/PycharmProjects/blog/blog/blog/templates/blog/post_list.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, '/home/ud/PycharmProjects/blog/blog/blog/templates/blog/post_detail.html', {'post': post})
My urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url('', views.post_list, name='post_list'),
url('post/<int:pk>/', views.post_detail, name='post_detail'),
]
And finally my myblog/urls.py
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', include('blog.urls')),
]
Please help me if you know the answer. I really tried everything but i cant find out my mistake.
Regards
The error message says
Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found.
Which shows that the pk is evaluating to the empty string ''.
Your URL tag is
{% url 'post_detail' pk=post.blog.pk %}
That means you are trying to pass the post's blog's pk. But you almost certainly want the post's pk instead:
{% url 'post_detail' pk=post.pk %}
Secondly, you are using the old url() with the path() syntax (new in Django 2.0). Your urls should be:
from django.urls import path
urlpatterns = [
path('', views.post_list, name='post_list'),
path('post/<int:pk>/', views.post_detail, name='post_detail'),
]
The problem might be due to the older version of Python or Django.
Try changing your My urls.py file as follow:
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
]
Here it will help to parse the address properly. If it still doesn't work the try to replace following in My post_list.html file
Old:
{% url 'post_detail' pk=post.blog.pk %}
New:
{% url 'post_detail' pk=post.pk %}
Hope this will help!
I'm using the view-callables in my app.urls because I keep getting warnings that app.views.view_name is being deprecated
app.urls
from .views import (
post_list,
post_detail,
post_create,
post_edit,
post_delete,
recent_posts,
)
urlpatterns = [
url(r'^$', recent_posts, name='recent_posts'),
url(r'^post/$', post_list, name="list"),
url(r'^post/create/$', post_create, name="create"),
url(r'^post/(?P<slug>[\w-]+)/$', post_detail, name="detail"),
url(r'^post/(?P<slug>[\w-]+)/edit/$', post_edit, name="edit"),
url(r'^post/delete/$', post_delete, name="delete"),
]
Here is my project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include ("blog.urls", namespace="post")),
]
here's the template:
<div class="menu-wrap">
<nav class="menu">
<ul>
<li>Home</li>
<li>All Posts</li>
{% if user.is_authenticated %}
<li> Create Post </li>
{% endif %}
</ul>
</nav>
</div>
This is what I have had to use but it's also bad, probably worse
<div class="menu-wrap">
<nav class="menu">
<ul>
<li>Home</li>
<li>All Posts</li>
{% if user.is_authenticated %}
<li> Create Post </li>
{% endif %}
</ul>
</nav>
</div>
Please any ideas on how to represent urls to my views in the templates? Thank you.
You should use the namespace name,in your case 'post' followed by colon (:) then view_name e.g to list all posts:
<li>All posts<li>
You can read more on how to handle urls in django documentation https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url
You want to link to the relevant URL using the url name that you declare in your urls.py file. So, your template should probably look like:
<div class="menu-wrap">
<nav class="menu">
<ul>
<li>Home</li>
<li>All Posts</li>
{% if user.is_authenticated %}
<li> Create Post </li>
{% endif %}
</ul>
</nav>
</div>
I am getting the following error message http://prntscr.com/7f3l4d everytime I click on a link in template.html. Can someone help me out with this.
urls.py project
urlpatterns = [
url(r'^', include('feature.urls', namespace="feature")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py app
urlpatterns = [
url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
views.py
def rock_and_feat(request):
feats = Feat.objects.order_by('-created')[:3]
rocks = Rockinfo.objects.order_by('-rank')[:50]
context = RequestContext(request, {
'feats': feats, 'rocks': rocks})
return render_to_response('template.html', context)
class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'
context_object_name = 'feat'
template.html
{% extends "index.html" %}
{% block mainmast %}
<div id="wrapper">
{% if feats %}
{% for feat in feats %}
<div class="specialsticky">
<img src="{{ feat.image.url }}" alt="some text">
<h1 class="mast-header">
{{feat.title}}
</h1>
</div>
{% endfor %}
{% else %}
<p>No </p>
{% endif %}
</div>
{% endblock %}
When I click on the image in template.html the error occurs.Thanks.
You have placed the urls for your app in a namespace feature, so when referring to that url, you must use the namespace.
url(r'^', include('feature.urls', namespace="feature")),
Change your template to: <a href="{% url 'feature:detail' feat.id %}"> and it will work.
https://docs.djangoproject.com/en/1.8/topics/http/urls/#url-namespaces
I was trying to add an editing/deleting function for a bookmarks app.
I get the following error:
My Urls.py file is the following:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'bookmarks.views.index', name='home'),
url(r'^bookmarks/$', 'bookmarks.views.index', name='bookmarks_view'),
url(r'^tags/([\w-]+)/$', 'bookmarks.views.tag'),
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})
url(r'^delete/(\d+)/$', 'bookmarks.views.delete'),
url(r'^edit/(\d+)/$', 'bookmarks.views.edit'),
)
The views.py file:
...remaining code
def delete(request, bookmark_id):
if request.method == 'POST':
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
b.delete()
return redirect(index)
def edit(request, bookmark_id):
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
context = {
'form' : BookmarkForm(instance=b),
}
return render(request, 'edit.html', context)
The bookmark widget in index.html
{% block bookmark_widget %}
{% if request.user %}
<div id="new-bookmark-widget">
<form method="post" action="{% url bookmarks.views.index %}">
{% csrf_token %}
<h3>Bookmark</h3>
{{ form.as_p }}
<p><button id="new-bookmark-submit">Submit</button>Submit</button>
</form>
</div>
{% endif %}
{% endblock %}
The relevant div in base.html
<div id="container">
<div id="header">
{% block bookmark_widget %}
{% endblock %}
<div id="authentication">
{% if user.is_authenticated %}
Hi {{user}}! Logout
{% else %}
Login
{% endif %}
</div>
<h1>My bookmarking app</h1>
</div>
<div id="content">
<h2>{% block subheader %}{% endblock %}</h2>
{% block content %}
Sample content -- you should never see this, unless an inheriting template fails to have any content block!
{% endblock %}
</div>
<div id="footer">
All copyrights reserved
</div>
</div>
The complete bookmark.html file:
<li>
<a class="bookmark-link" href="{{ bookmark.url }}">{% if bookmark.title %}{{ bookmark.title }}{% else %}{{ bookmark.url }}{% endif %}</a>
<div class="metadata"><span class="author">Posted by {{ bookmark.author }}</span> | <span class="timestamp">{{ bookmark.timestamp|date:"Y-m-d" }}</span>
{% if bookmark.tag_set.all %}| <span class="tags">
{% for tag in bookmark.tag_set.all %}
{{ tag.slug }}</span>
{% endfor %}
{% endif %}
</div>
{% if request.user.is_authenticated %}
<div class="actions"><form method=="POST" action="{% url bookmarks.view.delete bookmark.id %}>
{% csrf_token %}
<input type="submit" value="Delete">
</form> </div>
{% endif %}
I am new to Django, how do I handle error mistakes and how do I find the problem? I couldnt see it from the error message.
Thank you!
missing a comma on the following line:
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}), <-add here
I presume your comments section also lines up with the rest of your code and it is formatted incorrectly in your question
You're missing a comma at the end of this line:
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})
I'm new to Django and faced with next problem: when I turn on the appropriate link I get next error:
NoReverseMatch at /tutorial/
Reverse for 'tutorial.views.section_tutorial' with arguments '(1L,)' and keyword arguments '{}' not found.
What am I doing wrong? and why in the args are passed "1L" instead of "1"? (when i return "1" i get same error.) I tried to change 'tutorial.views.section_tutorial' for 'section-detail' in my template but still nothing has changed. Used django 1.5.4, python 2.7; Thanks!
tutorial/view.py:
def get_xhtml(s_url):
...
return result
def section_tutorial(request, section_id):
sections = Section.objects.all()
subsections = Subsection.objects.all()
s_url = Section.objects.get(id=section_id).content
result = get_xhtml(s_url)
return render(request, 'tutorial/section.html', {'sections': sections,
'subsections': subsections,
'result': result})
tutorial/urls.py:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
url(r'^$', views.main_tutorial, name='tutorial'),
url(r'^(?P<section_id>\d+)/$', views.section_tutorial, name='section-detail'),
url(r'^(?P<section_id>\d+)/(?P<subsection_id>\d+)/$', views.subsection_tutorial, name='subsection-detail'),
)
urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^tutorial/$', include('apps.tutorial.urls')),
)
main.html:
{% extends "index.html" %}
{% block content %}
<div class="span2" data-spy="affix">
<ul id="menu">
{% for section in sections %}
<li>
{{ section.name }}
<ul>
{% for subsection in subsections%}
{% if subsection.section == section.id %}
<li><a href=#>{{ subsection.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</li>
</ul>
</div>
<div class="span9">
<div class="well">
{% autoescape off%}
{{ result }}
{% endautoescape %}
</div>
</div>
{% endblock %}
You don't need $ identifier in url regex in your main urls file when including app urls:
url(r'^tutorial/$', include('apps.tutorial.urls')),
should be:
url(r'^tutorial/', include('apps.tutorial.urls')),