Django template not rendering at all - django

I have all my django views and templates working, however, when i tried to make this new, simple one it simply wouldn't render.
views.py:
def ListRefView2(request):
return render(request, "reference/test.html", context)
urls.py:
from django.urls import path
from .views import (
ListRefView,
ListRefView2,
CreateRefView,
DetailRefView,
UpdateRefView,
DeleteRefView,
)
urlpatterns = [
path('', ListRefView),
path('test2/', ListRefView2),
path('<str:slug>/delete/', DeleteRefView),
path('<str:slug>/edit/', UpdateRefView),
path('<str:slug>/', DetailRefView),
path('test2/', ListRefView2)
]
test.html
<h1>Hello </h1>
I am trying to get "Hello" to show up via the "ListRefView2" view

Related

How to import all url paths from urls.py and store it in a list in a view in views.py

I want to store in a dict all the urls defined in main_urlpatterns within url.py:
main_urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login_view, name='login')]
So in the view.py i created a function like follows:
from app_productora.urls import main_urlpatterns
def siteurls_view(request):
url_list=main_urlpatterns
context = {
'url_list':url_list
}
return render(request, 'siteurls.html', context)
But i have a circular problem between the import of the views.py in url.py and the import of the url.py in views.py.
Any idea?

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.

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