I'm trying to render index HTML and get post title from database but I'm getting error. I define in views post database but still getting error
name 'Post_title' is not defined
my app/views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import reverse
from .models import BlogPost,comments
def index(request):
Post_list = BlogPost.objects.all()
template_name = 'front/index.html'
return render(request, template_name,{Post_title:"Post_title",})
def post_detail(request):
return render(request, 'front/post_detail.html')
my app/urls.py
from django.urls import path
from .import views
urlpatterns = [
path('', views.index, name = 'index'),
path('<int:BlogPost_id>/', views.post_detail, name='Post Detail')
]
my project/urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from froala_editor import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('froala_editor/', include('froala_editor.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
my index.html template
<div class="col-md-8 mt-3 left">
{% for post in Post_list %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title">{{ post.Post_title }}</h2>
</div>
</div>
{% endfor %}
</div>
You are not sending the Post_list to the template through context. Send it like this
return render(request, template_name, {'Post_list':Post_list})
Related
The Form in register.html request for POST method but GET method is working. Submit is calling register method in views.py using GET method, but this shouldn't be happening.
Error :
Request Method: POST
Request URL: http://127.0.0.1:8000/Profile/register/register/
views.py
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.contrib.auth.models import User
from django.contrib import messages
def register(request):
if request.method == 'POST':
return redirect('/')
else:
return render(request, 'register.html')
register.html
<form action="register" method="post">
{% csrf_token %}
<input type="Submit">
</form>
urls.py of Project
from re import template
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
path('Profile/', include('Profile.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('login/', auth_views.LoginView.as_view(template_name="login.html"),
name='login'),
path('logout/', auth_views.LogoutView.as_view(),
name='logout'),
path('reset_password/', auth_views.PasswordResetView.as_view(),
name = 'reset_password'),
path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete')
]
urls.py of Profile app
from django.urls import path
from Profile import views
urlpatterns = [
path('profile/', views.profile, name='profile'),
path('register/', views.register, name='register'),
]
The URL is register/. Because you do not use a trailing slash, Django will make a redirect with a slash, but that will thus result in a GET request, not a POST request.
You thus should work with:
<form action="/profile/register/" method="post">
{% csrf_token %}
<input type="Submit">
</form>
or better is to work with the {% url … %} template tag [Django-doc]:
<form action="{% url 'register' %}" method="post">
{% csrf_token %}
<input type="Submit">
</form>
I am. trying to. build a search function for my django project base on the enter link description here
and the error message popped out "Reverse for 'Search' not found. 'Search' is not a valid view function or pattern name."
i have done the searching most of the advise asking me to check if the spelling is error
like my "search" in url or my search.html in my render
however, i have tried all the solution it still can't work
here is some of my code
urls.py:
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, vendor_data,search_product
urlpatterns = [
path('', home_view, name='home'),
path('contact/', contact_view),
path('about/', about_view),
path('create/', product_create_view),
path('product/', product_detail_view),
path('search/', search_product),
path('vendor/', vendor_data),
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
from .models import Product
from .forms import ProductForm, RawProductForm,VendorForm
def search_product(request):
if request.method == "POST":
query_name = request.POST.get('title', None)
if query_name:
results = Product.objects.filter(name__contains=query_name)
return render(request, 'products/search.html', {"results":results})
return render(request, 'products/search.html')
search.html
<!DOCTYPE html>
<html>
<head>
<title>Django Search</title>
</head>
<body>
<form action="{% url 'search' %}" method="POST">
{% csrf_token %}
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
{% for result in results %}
<p>{{result.name}}</p>
{% endfor %}
</body>
</html>
and following is my folder in case if needed
You need to specify the name of the urls.py with the name=… parameter:
urlpatterns = [
# …,
# name ↓
path('search/', search_product, name='search'),
# …
]
Change this file.. urls.py
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, vendor_data,search_product
urlpatterns = [
path('', home_view, name='home'),
path('contact/', contact_view),
path('about/', about_view),
path('create/', product_create_view),
path('product/', product_detail_view),
path('search/', search_product, name = 'search'), # changed here
path('vendor/', vendor_data),
path('admin/', admin.site.urls),
]
I am getting the below error when I am trying to load the home page:
Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name
My views.py file is as follows:
def home(request):
#query_results = QRC_DB.objects.all()
return render(request, 'display_data.html')
def display_data(request,component):
#query_results = QRC_DB.objects.all()
return HttpResponse("You're looking at the component %s." % component)
My urls.py file under the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
]
The urls.py file under the project is as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
And my html file (display_data) code is as follows :
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
Can anyone please help me to find out the mistake ?
Thanks.
Your urls.py file doesn't contain any url for display_data.
When you're trying to click the link rendered in the HTML tag, namely,
<li>SQL</li>
it tries to resolve the URL display_data.
First, it checks the root urls.py file. Among the following:
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
it matches the second one. Then it loads the fusioncharts.urls but the fusioncharts.urls doesn't contain any URL for display_data. That's why you are getting the error.
The urls.py file should be like this:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:arg>', views.display_data, name='display_data'),
]
# There is a change in urls.py and in your template 'display_data.html'
urls.py
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:component>', views.display_data, name='display_data'),
]
display_data.html
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
So, I have a code below:
newEra/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('mentor.urls')),]
mentor/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home_page),
]
my views.py
from django.shortcuts import render,redirect
from .models import *
from .forms import *
def home_page(request):
todo = ToDo.objects.all()
form = ToDoForms()
if request.method == 'POST':
form = ToDoForms(request.POST)
if form.is_valid():
form.save()
return redirect('')
context = {'todo' : todo, 'form':form}
return render(request, 'home.html',context)
home.html
<h1>To-Do list</h1>
<form action="POST" method="/">
{% csrf_token %}
{{form.name}}
<input type="submit" value="Create task" >
</form>
<ul>
{% for to in todo %}
<li>{{ to }}</li>
{% endfor %}
</ul>
But I am getting this error below
**Using the URLconf defined in NewEra.urls, Django tried these URL patterns, in this order:
admin/
The current path, POST, didn't match any of these.**
'mentor' file added to the SETTINGS
URLs are regexes.
Try:
urlpatterns = [
path('^/$',views.home_page),
]
urls.py
from django.conf.urls import include, url
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.conf import settings
from .views import Search
app_name = 'base'
urlpatterns = [
url(r'^index/$', Search.as_view(), name='index'),
url(r'^newresume/$', views.newresume, name='newresume'),
url(r'^profile/(?P<pk>[0-9]+)/$', views.profile, name='profile'),
url(r'^update_info/(?P<pk>[0-9]+)/$', views.update_info, name='update_info'),
]
views.py
def profile(request, pk):
student = get_object_or_404(Students, pk=pk)
return render(request, 'base/profile.html', {'student': student})
_tweet_search.html
<div class="studentfio">
<h4>{{ student.job }}</h4>
</div>
Console error
Hi all, help me with this error, please. I made a AJAX-request, after this i get an error, how can i fix that ?
<div class="studentfio">
<h4>{{ student.job }}</h4>
</div>
as you have mentioned app_name in your urls.py file, you need to mention that in your url