I am fairly new to Django and I am totally stuck on what is causing this error. I have done lots of searching but to no avail! Any help would be super appreciated.
The actual form works fine but when I try and submit the input data I get the error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='home']
^patientlist [name='patient_list']
^patientdetail/(?P<pk>\d+)/$ [name='patient_detail']
^add_patient/$ [name='add_patient']
The current URL, spirit3/add_patient/, didn't match any of these.
My urls.py in the mysite directory looks like:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('spirit3.urls')),
]
My urls.py in the app looks like:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^patientlist', views.patient_list, name='patient_list'),
url(r'^patientdetail/(?P<pk>\d+)/$', views.patient_detail, name='patient_detail'),
url(r'^add_patient/$', views.add_patient, name='add_patient'),
]
The relevant part of views.py:
def add_patient(request):
if request.method == 'POST':
form = PatientForm(request.POST)
if form.is_valid():
form.save(commit=True)
return redirect('home')
else:
print form.errors
else:
form = PatientForm()
return render(request, 'spirit3/add_patient.html', {'form':form})
And the html looks like:
{% extends 'spirit3/base.html' %}
{% block content %}
<body>
<h1> Add a Patient </h>
<form action="/spirit3/add_patient/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create Patient" />
</form>
</body>
{% endblock %}
Thanks in advance! :)
the form "action" attribute is wrong... seeing your urls configuration you dont have a /spirit3/add_patient/ url, I think It is /add_patient/
or you could just use a form tag without an "action" it will post to the current page:
<form role="form" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create Patient" />
</form>
Hope this helps
As pleasedontbelong mentionned, there's indeed no url matching "/spirit3/add_patient/" in your current url config. What you have in tour root urlconf is:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('spirit3.urls')),
]
This means that urls with path starting with "/admin/" are routed to admin.site.urls, and all other are routed to spirit3.urls. Note that this does NOT in any way prefixes urls defined in spirit3.urls with '/spirit3/', so in your case, all of these urls:
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^patientlist', views.patient_list, name='patient_list'),
url(r'^patientdetail/(?P<pk>\d+)/$', views.patient_detail, name='patient_detail'),
url(r'^add_patient/$', views.add_patient, name='add_patient'),
]
will be served directly under the root path "/" - ie, the add_patient view is served by "/add_patient/", not by "/spirit3/add_patient/".
If you want your spirit3 app's urls to be routed under "/spirit3/*", you have to specify this prefix in your root urlconf, ie:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^spirit3/', include('spirit3.urls')),
]
Note that you can use any prefix, it's totally unrelated to your app name.
As a last note: never hardcode urls anywhere, django knows how to reverse an url from it's name (and args / kwargs if any). In a template you do this with the {% url %} templatetag, in code you use django.core.urlresolvers.reverse().
Related
Disclaimer.....this is an assignment!
Can someone tell me why I am getting
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/%7B%20%25%20url%20'detail'%20user%20%25%20%7D
Using the URLconf defined in bobbdjango.urls, Django tried these URL patterns, in this order:
[name='home']
profile [name='profile']
user_detail/<str:user_id> [name='detail']
The current path, { % url 'detail' user % }, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I think its a problem with my URL but I am not seeing source of error. I would just like to click on the link on the home page and move to the user details page. See code below...
My Home page
<body>
{% block content %}
<h1>This is home page</h1>
<div>
<!-- Your profile -->
{% for user in users %}
<a href="{ % url 'detail' user % }">
<h3>{{user.first_name}}</h3>
</a>
{% endfor %}
</div>
{% endblock %}
</body>
My main URL
urlpatterns = [
## path('admin/', admin.site.urls),
path('', include('bobbpets.urls') ),
## path('bobbpets/<int:user.id/>', views.userDetails, name='userDetails'),
]
My app URL
urlpatterns = [
## path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('profile', views.profile, name='profile'),
path('user_detail/<str:user_id>', views.userdetail, name='detail'),
]
My Views
def home(request):
users = User.objects.all()
return render(request, 'home.html', {'users': users})
def userdetail(request,user_id):
user = User.objects.get(id=user_id)
return render(request, 'user_detail.html', {'user': user})
def profile(request):
return render(request, 'profile.html')
My Model
from django.db import models
class User(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
email=models.EmailField()
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
Your template URL should look like this: <a href="{% url 'detail' user %}
Remove the spaces that you have between your percent signs and parenthesis. That will cause an error.
Try this:
<a href="{ % url 'detail' user.id % }">
but I think you should change
'user_detail/<str:user_id>'
as
'user_detail/<int:user_id>'
I've been following the instructions here.
This is the error it gives me, when i try to run it on localhost:
Page not found (404)
Request Method: GET
Request URL: http://localhost:7000/account.html
Using the URLconf defined in gettingstarted.urls, Django tried these URL patterns, in this
order:
[name='index']
[name='account']
db/ [name='db']
admin/
^celery-progress/
The current path, account.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change
that to False, and Django will display a standard 404 page.
This is what i have in my urls.py
urlpatterns = [
path("", hello.views.index, name="index"),
path("", hello.views.account, name="account"),
path("db/", hello.views.db, name="db"),
path("admin/", admin.site.urls),
re_path(r'^celery-progress/', include('celery_progress.urls'))
]
This is what i have in views.py
def account(request):
if request.method == 'POST':
form = AccountForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('loading.html')
else:
form = Nameform()
return render(request, 'account.html', {'form': form})
Finally this is the form itself(account.html):
<form action="/account/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
I have the feeling i'm missing something really simple but i can't for the life of me see it. Any help would be greatly appreciated.
First, you need to change the URL patterns, because multiple views (hello.views.index and hello.views.account) are pointing towards the same pattern
urlpatterns = [
path("index/", hello.views.index, name="index"),
path("account/", hello.views.account, name="account"),
path("db/", hello.views.db, name="db"),
path("admin/", admin.site.urls),
re_path(r'^celery-progress/', include('celery_progress.urls'))
]
then, access the URL, http://localhost:7000/account/
You are requesting for the url that could match the string /account/ but in your urlpatterns variable you have an empty string so it can't match anything.
Remember that the first argument of urlpatterns is a pattern string that can be matched with regex.
Perhaps you could map it like:
path("/account/", hello.views.account, name="account")
I have just started on Django and am quite new to the whole thing.
I went through the whole tutorial on
https://docs.djangoproject.com/en/1.7/intro/tutorial03/ , which involves settings up the database and writing a simple form.
To start off my journey in Django, I plan to write a simple app that runs on localhost. And I have faced a issue in passing inputs form a form.
I have created a Name class in the models.py with 1 attribute
#name of the person
value = models.CharField(max_length=50)
In my index link: http://localhost:8000/helloworld/, it contains a simple 1-input-field form as follows:
<form method="post" action="{% url 'helloworld:hello' %}">
{% csrf_token %}
Enter Name: <input size="80" name="link" type="text">
<button type="submit">Submit</button>
</form>
The aim of the form is to return the input data to the same link (http://localhost:8000/helloworld/) with a input message of:
"Welcome [NAME], Hello World"
In my views.py, the following method is written:
def hello(request,name):
p = get_object_or_404(Link, pk=name)
try:
input_link = p.choice_set.get(pk=request.POST['link'])
except (KeyError, Link.DoesNotExist):
return render(request, 'helloworld/index.html',{
'error_message': "You did not enter a name",
})
else:
return HttpResponseRedirect(reverse('helloworld:index', args=(p.value)))
How ever if I access the page http://localhost:8000/helloworld/, and entered a data in the field and click submit, it brings me to the page
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/helloworld/url%20'helloworld:hello'
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^helloworld/ ^$ [name='index']
^helloworld/ ^(?P<pk>\d+)/$ [name='detail']
^helloworld/ ^(?P<pk>\d+)/results/$ [name='results']
^helloworld/ ^(?P<question_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, helloworld/url 'helloworld:hello', didn't match any of these.
The content in the urls.py was from https://docs.djangoproject.com/en/1.7/intro/tutorial04/#amend-urlconf
As requested, the content of urls.py:
from django.conf.urls import patterns, url
from domparser import views
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<question_id>\d+)/vote/$', views.vote, name='vote'),
)
May I know how do I go about solving this issue?
Thanks!
Simply an answer for this type. If you want to post to same url you are currently now.Try this and chenge def hello(request, name) to def hello(request).
action=""
Otherwise if urls.py
urlpatterns = patterns('app.views',
url(r'^$', view="index", name="app_index"),
)
Try this
action="{% url app_index %}"
As i found, in yours you can apply
action="{% url helloworld:index %}"
Hope this helps
For your answer updated. Try this
<form method="post" action="{% url "index" %}">
You need change this line
<form method="post" action="{% url 'helloworld:hello' %}">
to
<form method="post" action="{% 'helloworld:hello' %}">
this NoReverseMatch error is driving me nuts. I'm using Django 1.6 and I have checked through my urls and it just doesn't work. Please kindly guide me on this.
I basically want to do something deadly simple, just when I submit a html form, I get the data I want and then redirect to a result page, but it just doesn't work...
Here is my index.html file
<form name="input" action="{% url 'whatspring:sending' %}" method="post">
{% csrf_token %}
Recipient: <input type="text" name="usrname">
<br>
<input type="submit">
</form>
<br>
my view.py
def index(request):
return render(request,'springsend/index.html')
def sending(request):
var = request.POST['usrname']
doSomethinghere()
return HttpResponseRedirect(reverse('whatspring:results'))
def results(request):
return render(request,'springsend/send_results.html')
then my app urls.py
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
)
and the main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^send/', include('springsend.urls', namespace="whatspring")),
)
I have tried to look into the problem and it seems that the reverse function cannot get the name for the reverse url that I want (i.e. 'results' under the namespace 'whatspring'....)Am I missing something something trival? Please kindly help.
Your urls.py (springsend one) doesn't seem to have a url for the sending view, that's probably why {% url 'whatspring:sending' %} can't find it.
Simply change it to
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
url(r'^sending/$', views.sending, name='sending'), # this line
)
Every accessible view needs a url. The user's browser needs to have some address to send things. If it would just send it to your domain without url, Django would have no way to tell which url is requested. Django does not auto-generate these urls (which is probably good).
(The user himself does not need to know this url; you don't need to place any ` links anywhere.)
After getting post data from the following form, the page should redirect to 'associate:learn' as shown in the action. However, it just stays on the radio button page. I suspect I'm making a beginner's error but after rereading the tutorial, I'm not sure what's going on.
index.html
Choose a dataset
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'associate:learn' %}" method="post">
{% csrf_token %}
{% for dataset in datasets %}
<input type="radio" name="dataset" id="dataset{{ forloop.counter }}" value="{{ dataset.id }}" />
<label for="dataset{{ forloop.counter }}">{{ dataset }}</label><br />
{% endfor %}
<input type="submit" value="learn" />
</form>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
urls.py
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)),
)
views.py
def index(request):
images = Image.objects.all()
datasets = []
for i in images:
if i.rank() >= 3:
datasets.append(i)
return render(request, 'associate/index.html', {'datasets':datasets})
The original HTML should redirect to this page.
learn.html
THIS IS THE LEARN PAGE
Can you go to associate:learn directly?
In your first urls.py
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
The url will always match "associate.views.index" since it appears before "associate.views.learn" and they both have the same url.
You should change it to something like:
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^learn_or_something$', "associate.views.learn", name='learn'),
)
Hope this helps.
Your associate "index" and "learn" views both have the same URL. You need to have some way of distinguishing between them, otherwise the URL will always be served by the first one, which is index.