I was editing the template to include a hyperlink. But when I do I get NoReverseMatch error.
Reverse for 'views.hello_world' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
The template file:
layout.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/layout.css' %}">
</head>
<body>
<div class="site-container">
<nav>
Home [**Error here**]
</nav>
{% block content %}{% endblock %}
</div>
</body>
</html>
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
url(r'^courses/', include('courses.urls')),
url(r'^admin/', admin.site.urls),
url(r'^$', views.hello_world)
]
urlpatterns+=staticfiles_urlpatterns()
views.py
from django.shortcuts import render
def hello_world(request):
return render(request, 'home.html')
The line,
Home
when removed I don't get any error. But I add, the NoReverseMatch arises. What am I doing wrong?
You need to give the URL a name, and refer to that name in the url tag.
url(r'^$', views.hello_world, name='hello_world')
...
<a href="{% url 'hello_world' %}">
Related
I'm trying to inherit a base.html template to another one, but the thing is that when I extend it, it only shows the base.html but it doesn't show the block content, here's the code.
from django.contrib import admin
from django.urls import path
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.register, name = "register")
]
from django.shortcuts import render
# Create your views here.
def register(request):
return render(request, 'app1/register.html')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>lalala</h1>
</body>
</html>
{% extends "app1/base.html" %}
{% block content %}
<ol>
<li>hola</li>
<li>hola</li>
<li>hola</li>
</ol>
{% endblock %}
The thing is that it doesn't show the ol, it only shows the h1 from the base.html
I am trying to render a template inside the base html page. Base html has the header, footer, menu common for the whole site. The middle section of the page has to come from the child page. I think I have everything right, 200 OK, but it is not working, I don't see the content of the child page, form and div. I only see base.html for all requests. What am I missing?
project
|
---templates/base.html
---templates/childpage.html
base.html
{% load static %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="{% static 'css/custom.css' %}">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
childpage.html
{% extends "base.html" %}
{% block content %}
<form action="" method="">
</form>
<br><br><br>
<div class="something">
<a>some content</a>
</div>
{% endblock content %}
views
def home(request):
return render(request, 'base.html')
def askforchild(request):
return render(request, 'childpage.html')
urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', views.home, name='Site Home Page'),
url('askforchild', views.askforchild, name='child page'),
]
The url list for home page "" had to move to the bottom of the url list. Regex was catching all requests as home page requests. Catch all, an empty pattern should be at the bottom of the url list.
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 am stuck with this : < a href="{% url 'newapp:home' %}" > revWaves< /a>
Error message is : "NoReverseMatch"
Reverse for 'home()' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
I have created namespace for 'newapp' and url path is also correct i.e. /static/newapp/home.html and above < a href="{% url 'newapp:home' %}"> revWaves< /a> goes into header.html
So i am not able to figure out what is the error. Kindly help.
Main Urls.py
from django.conf.urls import url , include
from django.contrib import admin
urlpatterns = [
url(r'^newapp/', include('newapp.urls', namespace='newapp')),
]
newapp urls.py
from django.conf.urls import url , include
from . import views
urlpatterns = [
url(r'^$',views.home) ,
url(r'^about$',views.about) ,
url(r'^services',views.service) ,
url(r'^customers',views.customer),
url(r'^contact',views.contact) ,
]
views.py
from django.shortcuts import render
# Create your views here.
def home(request) :
return render(request, 'newapp/home.html')
def about(request):
return render(request, 'newapp/about.html')
def service(request):
return render(request, 'newapp/services.html')
def customer(request):
return render(request, 'newapp/customers.html')
def contact(request):
return render(request, 'newapp/contact.html')
header.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ady Rules</title>
<meta charset="utf-8"/>
{% load static %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Satisfy" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="{% static "newapp/style.css" %}"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.js"></script>
<style>
h1.intro {
color: brown;
}
</style>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- logo --!>
<div class="navbar-header">
<a class="navbar-brand" href="{% url 'newapp:home' %}"> revWaves</a>
<div>
</div>
</head>
<body class="body" style="background-color:#f6f6f6">
{% block content %}
{% endblock %}
</body>
</html>
home.html
{% extends "newapp/header.html" %}
{% block content %}
<h2>Hey There ! How Ya Doin ? </h2>
<h2> Welcome to the world of Programming !!</h2>
<h1 class = "intro"> New Appppppppp !!!!!!</h1>
{% endblock %}
I have the following problem in my Django project :
Error during template rendering
In template
/home/didier/Documents/Projects/inventory/inv/templates/index.html,
error at line 11 NoReverseMatch at /element Reverse for
'elements.views.element_detail' with arguments '()' and keyword
arguments '{u'pk': 1}' not found. 0 pattern(s) tried: []
Views.py is:
from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render, get_object_or_404
from .models import Element
def hello(request):
element = Element.objects.order_by('id')
template = loader.get_template('index.html')
title = 'Element list - RegiX'
context = {
'element': element,
'title': title
}
return HttpResponse(template.render(context, request))
ref element_detail(request, pk):
element = get_object_or_404(Element, pk=pk)
template = loader.get_template('element_detail.html')
context = {
'element': element
}
return HttpResponse(template.render(context, request))
Urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^element$', views.hello, name='hello'),
url(r'^detail/(?P<pk>[0-9]+)/$', views.element_detail, name='detail'),
]
index.html:
{% extends 'base.html' %}
{% load staticfiles %}
{% block page_title %}
Element list | RegiX
{% endblock %}
{% block content %}
<h2>Hello</h2>
{% for el in element %}
<li>
{{ el.manufacturer }} ::
<a href="{% url 'elements.views.element_detail' pk=el.pk %}">
{{ el.model }}
</a>
{{ el.date_purchase }} ::
<img src="{{ el.image.url }}">
</li>
{% endfor %}
{% endblock %}
base.html extend to index.html:
<!DOCTYPE html>
<html lang="en">
<head>
{% load staticfiles %}
<meta charset="UTF-8">
<title>{% block page_title %}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<header>
<strong>RegiX Enterprise</strong>
</header>
<section>
{% block login_content %}
{% endblock %}
{% block content %}
{% endblock %}
</section>
<footer><small>RegiX :: powered By Didier Zúñiga</small></footer>
</body>
</html>
Urls.py of settings:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('elements.urls', namespace='elements')),
url(r'^', include('userprofiles.urls', namespace='userprofiles')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I hope you can help me, thanks
Your URL reverse in the template is wrong. By giving it the name detail within a namespace, you need use that name to reverse the URL, not the name of the view.
So this:
<a href="{% url 'elements.views.element_detail' pk=el.pk %}">
Should be:
<a href="{% url 'elements.detail' pk=el.pk %}">