django form not calling html page - django

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"),
)

Related

Error The current path, didn't match any of these

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),

Django forms are not being processed with POST request

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!

Django Linking a html page to a view

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' %}'

what's wrong with my urls.py in this example?

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')
You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
the issue was I forgot to include the request in the return render.
The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')

Django NoReverseMatch Error

I'm trying to develop a blog script with Django. But when I want to show post links, I get NoReverseMatch error.
My views.py
# -*- coding: utf-8 -*-
# Create your views here.
from .models import Yazi, Yorum, Kategori
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext, loader
from django.contrib.sites.models import Site
def Home(request):
try:
posts = Yazi.objects.filter(yayinlanmis=True).order_by('-yayinlanma_tarihi')
except Yazi.DoesNotExist:
raise Http404
site = Site.objects.get_current()
c = RequestContext(request,{
'posts':posts,
'site':site
})
return render_to_response('Penguencik_Yazilar/yazi_list.html', c)
def Detail(request, slug):
post = Yazi.objects.get(slug=slug)
site = Site.objects.get_current()
c= RequestContext(request,{
'posts':post,
'site':site
})
return render_to_response('Penguencik_Yazilar/yazi_detail.html',c)
Urls.py in application folder.
from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
url(r'^$', views.Home,name='index'),
url(r'^/makale/(?P<slug>[0-9A-Za-z._%+-]+)$', views.Detail,name='detail'),
)
Urls.py in project folder
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('Penguencik_Yazilar.urls',namespace='blog')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And template code. What am I doing wrong?
{% load url from future %}
...
Read
Try to change this:
Read
to:
Read
Cause your view expect slug keyword here (?P<slug>[0-9A-Za-z._%+-]+).