Doesn't find a suitable url from the list(I assume it takes a string for a tuple)
NoReverseMatch at /decision/livingrooms/kitchen/
Reverse for 'style' with arguments '('provans',)' not found. 1 pattern(s) tried:
['decision/livingrooms/(?P[-a-zA-Z0-9_]+)/(?P[-a-zA-Z0-9_]+)/$']
Template
<ul class="menu menu_interiors">
{% for room in rooms %}
<li class="menu__item menu__item_interiors">{{ room.name }}</li>
{% endfor %}
</ul>
<ul class="menu menu_styles">
{% for style in styles %}
<li class="menu__item menu__item_interiors">{{ style.name }}</li>
{% endfor %}
</ul>
urls.py
from django.urls import path
from .views import ContactView
from . import views
app_name = 'decision'
urlpatterns = [
path('livingrooms/', ContactView.as_view(), name='livingrooms'),
path('livingrooms/<slug:rmslg>/', views.room, name='room'),
path('livingrooms/<slug:rmslg>/<slug:stslg>/', views.style, name='style'),
]
views.py
def room(request, rmslg):
styles = Room.objects.get(slug=rmslg).styles.all()
print(len(styles))
return render(request, 'decision/residentialInteriors.html', {"styles": styles})
def style(request, stslg):
style = Style.objects.get(slug=stslg)
return render(request, 'decision/residentialInteriors.html', {"style": style})
Change the last path to accept only one parameter.
From:
path('livingrooms/<slug:rmslg>/<slug:stslg>/', views.style, name='style'),
To:
path('styles/<slug:stslg>/', views.style, name='style'), # Since your view only accepts one parameter, the URL should capture only one as well.
Final Result:
urlpatterns = [
path('livingrooms/', ContactView.as_view(), name='livingrooms'),
path('livingrooms/<slug:rmslg>/', views.room, name='room'),
path('styles/<slug:stslg>/', views.style, name='style'), # Since your view only accepts one parameter, the URL should capture only one as well.
]
Related
I'm trying to create an individual page for each of my products. I found a solution online so I did this:
models.py:
#IMPORTS
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from multiselectfield import MultiSelectField
import uuid
class Product(models.Model):
product_id=models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
product_title=models.CharField(max_length=100)
product_price=models.FloatField(default=1.00)
image_one = models.ImageField(upload_to='media', default='def.jpg')
image_two = models.ImageField(upload_to='media', default='def.jpg')
image_three = models.ImageField(upload_to='media', default='def.jpg')
product_description = models.TextField()
#Toys
class Toy(Product):
CATS = (
("BT", "Boy Toys"),
("GT", "Girl toys"),
("BbyT", "Baby toys")
)
Category=models.CharField(max_length=10, choices=CATS)
views.py:
class ProductList(ListView):
model = Product
def Productdetail(request,pk):
product= Product.objects.get(id=id)
context = {'product': product}
return render(request, 'ecomm/product.html', context)
.
.
.
my urls.py:
from django.urls import path
from . import views
from .views import ProductList, Productdetail
urlpatterns = [
path('', views.home, name='ecomm-home'),
path('products/', ProductList.as_view()),
path('products/<int:pk>/', Productdetail, name='product_detail'),
....
product_detail.html:
{% extends "base.html" %}
{% block content %}
<h2>Product</h2>
{{product.product_title}}
{% endblock %}
girls_toys.html:
{% extends "ecomm/base.html" %}
{% block content %}
<style>
.prod{
text-align:center;
padding-left:50px;
width:500px;
display:block;
float:left;
padding-top:50px;
}
.header{
font-size:40px;
font-family:serif;
}
</style>
<center><h class="header"> Shop girl toys </h></center>
<br>
{% for t in toys %}
{% if t.Category == 'GT' %}
<div class="prod">
<img src="{{t.image_one.url}}" height="200px" width="250px">
<p> {{t.product_title}}</p>
<p> {{t.product_price}} EGP</p>
</div>
{% endif %}
{% endfor %}
{% endblock %}
I get this error:
Reverse for 'product_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['products/(?P<pk>[0-9]+)/$']
I think the issue is with the t.id or the <pk>, I'm not sure how to fix it tho. Any suggestions?
In views.py, product= Product.objects.get(id=pk), you are passing the value pk, so id=pk, not id=id
so i have this html :
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
<h1>{{ title }}</h1>
{% autoescape off %}
<p>{{ entry|safe }}</p>
{% endautoescape%}
<br>
<button type="button" name="button">Edit</button>
{% endblock %}
i want the url 'edit' to also get the value of the {{ title}} - instead of 'cat' ..i just used this value to test the rest of the code, but i don`t want to hard code it like that ...
here are my urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("<str:title>", views.title, name="title"),
path("edit/<str:title>", views.edit, name="edit"),
path("error/", views.error, name="error"),
path("new/", views.new, name="new"),
]
this is the code in views.py :
def title(request, title):
entry = util.get_entry(title)
if entry == None:
return redirect("error")
entry_html = md.convert(entry)
return render(request, "encyclopedia/title.html", {
"entry": entry_html
})
def edit(request, title):
if request.method == "POST":
# get the changes from the user
form = EditEntryForm(request.POST)
if form.is_valid():
content = form.cleaned_data["content"]
# save the entry and redirect to the updated page
util.save_entry(title, content)
return redirect("title", title=title)
else:
redirect("edit", title=title)
entry = util.get_entry(title)
form = EditEntryForm(initial={'content': entry})
return render(request, "encyclopedia/edit.html", {
"form": form
})
as i said the code works as intended if i hard code the value of the title ....how do i get {{ title }} in the href url ?
You should just be able to pass it in as:
{% url 'edit' title %}
…or you may need to specify the argument name:
{% url 'edit' title=title %}
See this link in the documentation for more information.
How to handle the variable inside the template tag {% %},I need to give arguments to my function send_mail_view()
urls.py
urlpatterns = [
path('send_mail_view/<sender_email>/<receiver_email>/<doc>',views.send_mail_view, name='send_mail_view'),
]
mailing_app/views.py
def send_mail_view(request,sender_email,receiver_email,doc):
email=EmailMessage( 'i want to hire you Nitish ', 'please provide your resume', sender_email, [receiver_email] )
email.attach_file('mailing_app/179.pdf')
res=email.send(fail_silently=False)
success_msg="mail sent success fully"
return render(request,'mailing_app/success.html',{'success_msg':success_msg})
student_app/views.py
def student_main_view(request,username):
user_obj=User.objects.filter(username=username)
pdf_student_model_obj=pdf_student_model.objects.filter(username__in=user_obj)
pdf_list=[]
for p in pdf_student_model_obj:
print("nikseva",type(p.pdf_id))
pdf_indexing_model_obj=pdf_indexing_model.objects.filter(pdf_id=str(p.pdf_id))
for x in pdf_indexing_model_obj:
pdf_list.append({'pdf_id':x.pdf_id,'pdf_title':x.pdf_title,'pdf_abstract':x.pdf_abstract,'pdf_path':x.pdf_path,'sender_email':"n#gmail.com",'receiver_email':username,})
print("pdf_list",pdf_list)
return render(request,'rnd_app/student_mainpage.html',{'pdf_list':pdf_list})
templates/mail.html
{% for z in pdf_list %}
pdf_title :{{z.pdf_title}}
pdf_abstract:{{z.pdf_abstract}}<br>
pdf_path:{{z.pdf_path}}
Send Email
{% endfor %}
You don't need the variable braces when you're already inside a template tag. Just reference them directly:
{% url 'mailing_app:send_mail_view' request.user.username z.receiver_email z.pdf_path %}
I am encountering the following error when trying to set DetailView for each of the post in my postlist:
NoReverseMatch at /blog/post-list/ Reverse for 'postdetail' with
keyword arguments '{'id': ''}' not found. 1 pattern(s) tried:
['blog\/post-list/(?P\d+)/$']
Here the code:
views.py
def PostList(request):
postlist = Post.objects.all()
context = {
"postlist":postlist
}
template_name = "postlist.html"
return render(request,template_name,context)
def PostDetail(request,id):
post = get_object_or_404(Post,id=id)
context = {
'post':post
}
template_name = "postdetail.html"
return render(request,template_name,context)
postlist.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<div class="col">
{% for item in postlist %}
<ul>
<li><h3>Title:</h3> {{ item.title }} <smal>{{ item.timestamp }}</smal></li>
<li><h3>description:</h3>{{ item.description }}</li>
<li><h3>Updated:</h3>{{ item.updated }}</li>
<li><h>Author: {{ item.Author }}</h></li>
{{ item.id }}
</ul>
{{ item }}
{% endfor %}
</div>
</div>
</div>
{% endblock %}
urls.py
urlpatterns = [
re_path(r'^post-list/$',views.PostList,name ='postlist'),
re_path(r'^post-list/(?P<id>\d+)/$',views.PostDetail,name="postdetail"),
path ('post-create',views.CreatPost, name='post-create'),
re_path (r'^post-list/update/(?P<id>\d+)/$',views.PostUpdateis,name='update-post'),
# re_path(r'^(?P<slug_post>[-\w])+/$',views.PostDetail,name="postdetail"),
]
Does anybody know how to solve this?
I was trying to build a django app which would let me access some groups that contain student(elev) objects. I can't make Elev objects show, tho.
Elevi urls.py
from django.conf.urls import url
from . import views
app_name = 'elevi'
urlpatterns = [
#/
url(r'^$', views.index, name = 'index'),
#/Grupe/id
url(r'^grupe/(?P<grupa_id>[0-9]+)/$', views.connect, name="connect"),
#/Grupe/id/elevi_id
url(r'^grupe/(?P<grupa_id>[0-9]+)/(?P<elev_id>[0-9]+)/$', views.elev_individual, name="elev_individual")
]
Elevi views.py
from django.shortcuts import render,get_object_or_404
from .models import Grupa,Elev
def index(request):
toate_grupele = Grupa.objects.all()
context = {"toate_grupele" : toate_grupele}
return render(request,'Elevi/test.html',context)
def connect(request,grupa_id):
grupa = get_object_or_404(Grupa, pk = grupa_id)
return render(request,'Elevi/connect.html',{'grupa':grupa})
def elev_individual(request,elev_id):
elev = get_object_or_404(Elev, pk = elev_id)
return render(request,'Elevi/elev_individual.html',{'elev':elev})
test.html
{% if toate_grupele %}
<ul>
{% for grupa in toate_grupele %}
<li>{{ grupa.nume_grupa }}</li>
{% endfor %}
</ul>
{% else %}
<p>Nu sunt grupe salvate</p>
{% endif %}
connect.html
<h1>{{ grupa.nume_grupa }}</h1>
<h3>Lista Elevi:</h3>
<ul>
{% for elev in grupa.elev_set.all %}
<li>{{ elev.nume_elev }} {{ elev.prenume_elev }}</li>
{% endfor %}
</ul>
elev_individual.html
<p>{{ elev.nume_elev }} {{ elev.prenume_elev }}</p>
The url pattern doesn't match the view arguments
view
def index(request): #url pattern don't need any argument
def connect(request,grupa_id): #url pattern needs one argument
def elev_individual(request,elev_id): #requires one argument
url
url(r'^$', views.index, name = 'index'), # good
url(r'^grupe/(?P<grupa_id>[0-9]+)/$', views.connect, name="connect") #good
If you look at the error message, elev_individual requires two argument in the def elev_individual(request,elev_id): view method. To fix this, either remove the second argument so that
['grupe/(?P<grupa_id>[0-9]+)/(?P<elev_id>[0-9]+)/$']
becomes
['grupe/(?P<elev_id>[0-9]+)/$']
or add another argument elev_id as the second argument to your view so it becomes
def elev_individual(request,grupa_id, elev_id):
and filter as necessary