Django Linking a html page to a view - django

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

Related

im getting error in django Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/about/

code of project named carparts urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('parts.urls')),
]
code for my app named parts urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('about', views.about,name='about'),
path('contact', views.contact,name='contact'),
]
code for views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request, 'index.html')
# return HttpResponse('this is home page')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
index page code output is show in http://127.0.0.1:8000 but my about and contact page shows Page not found (404) . Can any one help me with this code.
Thank you
Joint '/' in your both urls.py like that:
urlpatterns = [
path('', views.index,name='index'),
path('about/', views.about,name='about'),
path('contact/', views.contact,name='contact'),
]
According Django Design philosophy on Definitive URLs:
Technically, foo.com/bar and foo.com/bar/ are two different URLs, and
search-engine robots (and some Web traffic-analyzing tools) would
treat them as separate pages. Django should make an effort to
“normalize” URLs so that search-engine robots don’t get confused.

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 form action url 'abc' is redirect to abc twice

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.

How to navigate from current html page to another html page in django

I'm working on django app and i'm facing a problem when i need to navigate from my index.html page to another about.html page. I'm setting everything in this way below:
urls.py (myproject)
from django.urls import path, include
urlpatterns = [
path('', include('weather_app.urls')),
path('about/', include('weather_app.urls'))
]
urls.py (myapp)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name="about")
]
In index.html, everything is working well, so i will not put the code.
and in view.py:
from django.shortcuts import render
import requests
def about(request):
return render(request, "about.html")
i have the code below in my index.html:
About
and i cannot get about.html page when click About link as you can see above.
Can anybody help me?
Thanks in advance..
You have to use this tag to avoid manual rendering of urls.
In your code:
About
Let me know if this works!

django form not calling html page

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