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
Related
So I am new to Django and I'm trying to create a HTML form (just following the tutorial for the name input) and I can input the name but can't direct to the /thanks.html page.
$ views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
print(form)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/polls/thanks.html')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
$ name.html
<html>
<form action="/polls/thanks.html" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
<html>
$ /mysite/urls
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
$ mysite/polls/urls.py
from django.urls import path
from polls import views
urlpatterns = [
path('', views.get_name, name='index'),
]
When I go to to the page, I can enter my name fine but then when I submit i get
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/ [name='index']
admin/
The current path, polls/thanks.html, didn't match any of these.
Even though thanks.html is inside /polls
Sorry if the fix is super easy I just haven't used Django before.
Thanks :)
Create a view called thanks in views.py
def thanks(request):
return render(request, 'thanks.html')
Now, Link the /poll/thanks/ url to the thanks template by adding path('thanks/', views.thanks, name='thanks') to your urls.py of the polls app.
$ mysite/polls/urls.py
from django.urls import path
from polls import views
urlpatterns = [
path('thanks/', views.thanks, name='thanks'),
]
Finally change the following line in your get_name view
return HttpResponseRedirect('/polls/thanks/')
Change your main urls.py:
url(r'^polls/', include('polls.urls')),
And in your app's urls.py:
url(r'^$', views.get_name, name='index'),
And in your views.py change to:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return render(request, 'thanks.html')
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'))
I'm trying to import a simple single-field form in Django, and I have gone through plenty of Tutorial Videos on YouTube describing the same. Yet, I'm unable to render the simple form on my web-app. I'm pretty sure I'm doing something really stupid that is still oblivious to me.
I'll also post in the folder structure so you can suggest if I'm defining the class/function in the wrong views.py file.
The appropriate source codes are as follows:
earthquake/views.py File
from django.shortcuts import render
from earthquake.forms import HomeForm
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'earthquake/home.html'
def get(self, request, *args, **kwargs):
form1 = HomeForm()
argf = {
'myform': form1
}
return render(request, self.template_name, argf)
forms.py
from django import forms
class HomeForm(forms.Form):
post = forms.CharField()
home.html (snippet)
<div class="container">
<div class="jumbotron">
<h1>Query Form</h1>
<p>Please Enter the parameters you want to query against the USGS Earthquake DB</p>
<div class="container">
<form class="" method="post" action="">
{% csrf_token %}
{{ myform }}
<button type="submit" class="btn btn-success">Search</button>
</form>
</div>
</div>
</div>
Django Project urls (interview.py/urls.py)
from django.contrib import admin
from django.urls import path, include
from interview.views import login_redirect
from interview import views
from django.contrib.auth.views import LoginView
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('', login_redirect, name='login_redirect'),
path('admin/', admin.site.urls),
path('home/', include('earthquake.urls')),
path('login/', LoginView.as_view(template_name='earthquake/login.html'), name="login"),
path('logout/', LogoutView.as_view(template_name='earthquake/logout.html'), name="logout"),
path('register/', views.register, name='register'),
]
App URLS (interview/earthquake/urls.py)
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Folder Structure
https://i.stack.imgur.com/zoehT.jpg
(In case you're unable to see the last entry in the image, it's the main views.py present in the project folder).
The following is the snapshot of the render I currently get:
https://i.stack.imgur.com/kXR7W.jpg
I see that in your home views file your class based view is called
HomeView(TemplateView)
Yet in your app urls you are including the view as view.home when it should be
view.HomeView
to add to that, this is a classed based view so your urls page for that should look like:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home.as_view(), name='home'),
]
Since this is a class based view.
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 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.