I know that this is a common issues, but none of the answers which I found here helped me out.
I cannot figure out what is wrong here (Yes I tried with '' and without them in url)
Here's what I got so far
template:
<html>
<body>
<div> Link here </div> {{ formText }}
</body>
</html>
url(own config)
from django.conf.urls import patterns, include, url
from metadaten import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
root url:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^metadaten/', include('metadaten.urls', namespace='metadaten')),
url(r'^admin/', include(admin.site.urls)),
)
views:
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render, get_object_or_404
from metadaten.models import Title
from django.core.urlresolvers import reverse
def index(request):
return render(request, 'metadaten/index.html', {'formText' : 'foo'})
error message:
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
Any suggestions why I'm not able to build a simple href using {% url %} ?
please don't blame me if this question might be easy to figure out :(
You used a namespace with metadaten. You'll want to use {% url 'metadaten:index' %}.
Look at the last example for the url tag.
Related
To elaborate, I went down a bit of a rabbit hole just trying to make a trailing "/" optional in the URL. got it to sorta work with the project directory, but when on the blog app, I tried to use re_path in order to use regex to allow for an optional trailing "/" in the url, but I seem to get an error in the template
website urls.py (directory with settings.py)
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('blog/', include('blog.urls')),
path('blog', include('blog.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
blog urls.py
from django.urls import path, include, re_path
from .views import BlogView, ArticleView
urlpatterns = [
path('', BlogView.as_view(), name="blog"),
re_path(r'titles/(?P<slug_url>\d+)/?$', ArticleView, name="article")
]
blog.html template error appears at the href
{% extends 'base.html' %}
{% block content %}
<h1>Articles</h1>
{% for post in object_list %}
<h2>{{ post.title }}</h2>
{% endfor %}
{% endblock %}
blog views.py
from django.shortcuts import render, get_object_or_404, redirect
from .models import Post
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect
# Create your views here.
class BlogView(ListView):
model = Post
template_name = 'blog.html'
def ArticleView(request, url_slug):
post = get_object_or_404(Post, url_slug = url_slug)
return render(request, 'article.html', {'post':post})
Any help would be greatly appreciated, I just find the 404 I get without a trailing / to be very annoying
I am new to Django and I was working at my first project, but I got:
NoReverseMatch with Exception Value: 'hiring_log_app' is not a
registered namespace in base.html
which follows:
href="{% url 'hiring_log_app:index' %}" >Hiring Log
href="{% url 'hiring_log_app:topics' %}" >Topics
I made sure my namespace was correct and I looked at the other topics already opened without figuring out how to solve the issue.
I paste urls.py:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include ('hiring_log_app.urls', namespace='hiring_log_app')),
hiring_log_app/urls.py:
"""Define URL patterns for hiring_log_app"""
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^topics/$', views.topics, name='topics'),
]
views.py:
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for hiring_log_app"""
return render(request, 'hiring_log_app/index.html')
def topics(request):
""" list of topics"""
topics = Topic.objects.raw( "SELECT text FROM HIRING_LOG_APP_TOPIC;")
context = {'topics' : topics}
return render(request, 'hiring_log_app/topics.html', context)
Does anyone know where I am making a mistake?
You have wrongly specified the namespace in urlpatterns. Follow the pattern below:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
Edit:
If you are using Django 2.2.4 then you should use path instead of url since the usage of it is replaced by re_path.
from django.contrib import admin
from django.conf.urls import include
from django.urls import re_path
urlpatterns = [
re_path(r'^admin/', include(admin.site.urls)),
re_path(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
Having the below error while trying to dispatch two variables from my template to my view via url dispatching. I dont understand why the url is not able to be found. This is a snippet of my entire code, but I am pretty sure that the issue lies in the below. This is within an application within my project
NoReverseMatch at /home/patientid=1002411102/clinicid=1007/
Reverse for 'patient_summary' with no arguments not found. 1 pattern(s) tried: ['home/patientid=(?P<patient_id>\\d+)/clinicid=(?P<clinic_id>\\d+)/$']
index.html
<tbody>
{% for patient in patients %}
<tr class="gradeX">
<td>{{patient.PatientID}}</td>
<td>{{patient.ClinicID}}</td>
<td>{{patient.PatientFirstName}}</td>
<td>{{patient.PatientMidName}}</td>
<td>{{patient.PatientLastName}}</td>
<td>{{patient.PatientDOB}}</td>
<td>40/30</td>
<td>192</td>
<td>100</td>
<td>70</td>
<td>23m</td>
<td style="color:red">Abnormal</td>
<td><a style="color:#4A90E2; cursor: pointer" href="{% url 'home:patient_summary' patient_id=patient.PatientID clinic_id=patient.ClinicID %}">View/Edit</a></td>
</tr>
{% endfor %}
</tbody>
urls.py
from django.conf.urls import url
from home import views
app_name = 'home'
urlpatterns = [
url('^$', views.index, name='index'),
url('patientid=(?P<patient_id>\d+)/clinicid=(?P<clinic_id>\d+)/$', views.patient_summary, name='patient_summary'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template.context_processors import request
from django.contrib.auth.decorators import login_required
import requests
import json
#login_required
def index(request):
payload = {"ClinicId":"1007"}
r = requests.post("https://hidden", data=payload)
jsonList = r.text
data = json.loads(jsonList)
# print(data[0]["PatientID"])
patients = []
for patient in data:
patients.append(patient)
my_dict = {'patients': patients, 'homeIsActive': 'active'}
return render(request, 'home/index.html', my_dict)
def patient_summary(request, patient_id, clinic_id):
my_dict = {'homeIsActive': 'active', 'chartData': [[0,1],[1,1],[2,1]]}
return render(request, 'home/patient_summary.html', my_dict)
urls.py for base project
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls import url
from login import views as login
from home import views as home
from enrollment import views as enrollment
from inventory import views as inventory
urlpatterns = [
url('^$', login.user_login, name='login'),
url('^logout/', login.user_logout, name='logout'),
url('^home/', include('home.urls')),
url('^enrollment/', enrollment.index, name ='enrollment'),
url('^inventory/', inventory.index, name ='inventory'),
# url('^settings/', settings.index, name='settings'),
url('^settings/', include('settings.urls')),
path('admin/', admin.site.urls),
]
I was calling the url name of patient_summary from another location so thats why I kept getting the error. i think rule of thumb is always use relative url paths
I'm trying to run a blog build with django on the browser. And I got this error:
NoReverseMatch at /
Reverse for 'blog.views.post_detail' not found.
'blog.views.post_detail' is not a valid view function or pattern name.
My url.py of my app looks like:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
]
It seems that when I type 127.0.0.1:8000/.
The url will direct to views.post_list.
And my views.py looks like:
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.filter(published_date__isnull=False)
return render(request, 'blog/post_list.html', {'posts': posts}
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
post_list() will render the request with post_list.html.
Inside post_list.html, the error comes from the line:
<h1>{{ post.title }}</h1>
I don't really understand what 'Reverse' means in the error message. 'blog.views.post_detail' does exist in views.py. I think I got everything needed for the code and can't figure out what went wrong.
I'm new to django, sorry if the question is basic and thanks for answering!
Django 1.10 removed the ability to reverse urls by the view's dotted import path. Instead, you need to name your url pattern and use that name to reverse the url:
urlpatterns = [
url(r'^$', views.post_list, name='post-list'),
url(r'^(?P<pk>\d+)/$', views.post_detail, name='post-detail'),
]
And in your template:
<h1>{{ post.title }}</h1>
It seems that your urls.py should be as follows:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^(?P<pk>\d+)/$', views.post_detail),
]
You should define a name for your url:
urlpatterns [
url(r'^$', views.post_list,name=post_list),
]
then use url tag like this:
AppName is you django application name.
I have looked around and can't really find a solution to my problem. Here is the error django throws. This error is being thrown when on my homepage I have a fiew links that upon clicking should direct you to a details view of said link.
Using the URLconf defined in untitled.urls, Django tried these URL patterns, in this order:
^$
^index/ ^$ [name='index']
^index/ ^(?P<distro_id>[0-9]+)/$ [name='distro_id']
^admin/
The current URL, index//, didn't match any of these.
To my knowledge I don't understand why this error is being thrown.
Here is my urls.py
from django.conf.urls import include, url
from django.contrib import admin
import index.views
urlpatterns = [
url(r'^$', index.views.index),
url(r'^index/', include('index.urls', namespace='index')),
url(r'^admin/', admin.site.urls),
]
My index/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# /index/
url(r'^$', views.index, name='index'),
#/distro/123/
url(r'^(?P<distro_id>[0-9]+)/$', views.detail, name='distro_id'),
]
My views.py
from django.shortcuts import get_object_or_404, render
from django.template import loader, RequestContext
from django.http import Http404
from .models import Distros
def index(request):
all_distros = Distros.objects.all()
context = {'all_distros': all_distros, }
return render(request, 'index/index.html', context)
def detail(request, distro_id,):
distro_id = get_object_or_404 (Distros, pk=distro_id)
return render(request, 'index/detail.html', {'distro_id': distro_id})
template code:
{% extends 'index/base.html' %}
{% block body %}
<ul>
{% for distro in all_distros %}
<li>{{ index.distro_id }}</li>
{% endfor %}
</ul>
{% endblock %}
I believe those are all the relevent files. I believe everything is setup correctly so I am not sure why the error is being thrown. I'm sure im missing something simple that i'm just overlooking.
Please don't use hardcoded URLs as they are error prone as in your situation. Instead of:
<a href="/index/{{ index.distro.id }}/">
use the url template tag with your namespace (index) and view name (distro_id):
<a href="{% url 'index:distro_id' index.id %}">
Note that you also have an error with index.distro.id as index is actually a Distros object. It has an id field, but not distro.id.