I'm trying to save the data submitted on a form, and redirect the user to a results.html.
------- Front template -------------------
Welcome to readability test V1
<form action="/addtext" method="POST">{% csrf_token %}
<input type="text" name="title"/>
<input type="text" name="content"/>
<input type="submit" value="Add"/>
</form>
---------- URLS.PY -----------------
from django.contrib import admin
from django.urls import path
from readtest.views import readtest, addtext
urlpatterns = [
path('admin/', admin.site.urls),
path('readability-test/', readtest),
path('results/', addtext),
enter code here
------------ VIEWS.PY ------------------------
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import TestedText
# Create your views here.
def readtest(request):
return render(request, 'test.html')
def addtext(request):
a = TestedText(title=request.POST['title'], content=request.POST['content'])
a.save()
return HttpResponseRedirect('/results/')
------ ERROR IM GETTING ------------------
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/addtext
Using the URLconf defined in redability.urls, Django tried these URL patterns, in this order:
admin/
readability-test/
results/
The current path, addtext, didn't match any of these.
Update your urls.py file line path('results/', addtext) to path('addtext/', addtext)
from django.contrib import admin
from django.urls import path
from readtest.views import readtest, addtext
urlpatterns = [
path('admin/', admin.site.urls),
path('readability-test/', readtest),
path('addtext/', addtext),
Related
I have written the most basic Django application to understand forms as below. When I enter the required data into the fields and press Submit, the code after "if request.method == 'POST'" is ignored. I am redirected to the appropriate page and an entry with the first name "John" and last name "Smith" is created in my database. As you can see in the code below, this object should only be created if the request method is not POST. I know that I have set the request method to POST because that is what is shown on my CMD so what is happening??
Here is my template 'index.html':
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the site</title>
</head>
<body>
<form action="thanks/" method='POST'>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" vlaue="Submit">
</form>
</body>
</html>
Here is my views.py file:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import NewObjectForm
from .models import Object
# Create your views here.
def index(request):
if request.method == 'POST':
form=NewObjectForm(request.POST)
if form.is_valid():
first_name=form.cleaned_data['first_name']
last_name=form.cleaned_data['last_name']
a=Object.create(first_name=first_name,last_name=last_name)
a.save()
return HttpResponseRedirect('/thanks/')
else:
new=Object.create(first_name="Not",last_name="Valid")
new.save()
else:
#Code which is run if the request.method is not equal to 'POST'
form=NewObjectForm()
newer=Object.objects.create(first_name="John",last_name="Smith")
newer.save()
return render(request,'formapp/index.html',{'form':form})
def end(request):
return render(request,'formapp/thanks.html')
Here is the urls.py file from the main project:
"""experimentalForms URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('',include('formapp.urls')),
path('admin/', admin.site.urls),
]
And here is the urls.py file from my application:
'''
URLs for formapp
'''
from django.urls import path
from . import views
app_name="formapp"
urlpatterns=[
path('',views.index,name='index'),
path('thanks/',views.end),
]
Thanks #mbieren! I have changed my the action attribute of my form and it is working perfectly!
In django index.html, I have below code
<form class="selectedPizza" action="{% url 'cost' %}" method="POST">
<!--Do some action here-->
<form>
In my app(order) urls.py
from django.urls import
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("cost/", views.cost, name="cost")
]
In main site(pizza) urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("", include("orders.urls")),
path("cost/", include("orders.urls")),
path("admin/", admin.site.urls),
]
In views.py
def index(request):
#some action here
return render(request, 'orders/index.html', context)
def cost(request):
if (request.method == 'POST'):
#some action here
return render(request, 'orders/cost.html', context)
After submitting the form I am redirected to "http://127.0.0.1:8000/cost/cost/".
I am learning Django and not able to find a possible way to get this form to redirect to cost.html page after submitting
I don't know why you have included "orders.urls" twice in your main urls. Remove the second one. "cost" is part of the included URL, you don't need it in the main one as well.
NoReverseMatch found for a url i have tried and
{% url '/feedback/form/' %}
i am using a feedback app.
#feedback\urls.py
from django.urls import path, include
from .views import request_feedback
urlpatterns = [
path('form/', request_feedback, name = 'get_feedback' ),
]
#feedback\views.py
def request_feedback(request):
if request.method == 'POST':
feedback_form = FeedBackForm(request.POST)
if feedback_form.is_valid():
feedback_form.save()
return redirect('')
else:
feedback_form = FeedBackForm()
return render(request, 'feedback/feedbackform.html', {'feedback_form':feedback_form})
#webapp\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('feedback/', include("feedback.urls")),
path('user/',include("user.urls")),
]
i am getting this error
NoReverseMatch at /feedback/form/
Reverse for '' not found. '' is not a valid view function or pattern name
i am using python 3.7 and django 2
i do mitigate this problem
So I know that there are other people who have asked the same question, and I have read through them. However, the solutions provided there are giving me a strange error, and I would appreciate any help in understanding it.
So here's my home.html file:
<head>
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
<!-- Sign Up -->
Sign Up
</body>
And here's my views.py:
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, "home.html")
def signup(request):
return render(request, "signup.html")
And here's my urls.py:
from django.conf.urls import url
from .views import home, signup
urlpatterns = [
url(r'^signup/', signup, name="signup"),
url(r'^', home, name="home"),
]
Thank you for all the help :)
Edit:
The error message is
Reverse for 'signup' not found. 'signup' is not a valid view function or pattern name.
Also I actually changed the way I did urls.py. Now, I only have one urls.py in my main mysite folder:
from django.conf.urls import url, include
from django.contrib import admin
from home import views
from accounts import views as accountsViews
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home),
url(r'^signup', accountsViews.signup),
]
Your second url in 'urls.py' does not have a name.
The url tags would not be able to find them by name -- '{% url 'signup' %}'
I am learning django and started with just a small login authentication system. I created the html page for login :
<html>
<title>Login</title>
<body>
<br><br><br><br><br><br><br><br><br><br>
<form name="login" action = "/loginr/" method = "get">
USERNAME:<input type="text" name="uname"><br>
PASSWORD:<input type="password" name="passwd"><br>
<input type = "submit" value = "Login">
</form>
</body>
</html>
my views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request,'login_page.html')
x=''
def process(request):
global x
x=request.GET
return HttpResponseRedirect('/login/thanks/')
def thanks(request):
return render(request, 'thanks.html')
my urls.py:
from django.conf.urls import patterns,url
from login import views
urlpatterns=patterns('',
url(r'^$',views.index, name="index"),
url(r'^loginr/',views.process),
url(r'^login/thanks/',views.thanks)
)
outermost mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^login/', include('login.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^loginr/', include('login.urls')),
url(r'^login/thanks/',include('login.urls')),
)
The above codes show no error. But when i fill the login form and click login, the page itself reloads. I want the "thanks.html" to load. What is the mistake am I doing?
In views.py, you didn't import HttpResponseRedirect.
from django.http import HttpResponseRedirect
UPDATE
Change mysite/urls.py as follow:
...
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('login.urls')),
)
Change login/urls.py as foolow:
...
urlpatterns=patterns('',
url(r'^loginr/?$',views.process),
url(r'^login/thanks/?$',views.thanks)
url(r'^login/?$',views.index, name="index"),
)