I am following this tutorial https://tutorial.djangogirls.org/en/extend_your_application/ but getting Templatesyntax error when trying to pass pk from html to url using path method.
With what i have read about this error this has something to do with braces and quotes but in this case i am not able to figure out the exact problem with the syntax.
This the listview.html
{% for vehicle_list_load in vehicle_list_loads %}
<tr>
<td>{{vehicle_list_load.vehicle_num}}</td>
<td>{{vehicle_list_load.Driver_name}}</td>
<td>{{vehicle_list_load.BusinessUnit}}</td>
<td>{{vehicle_list_load.CheckinTime}}</td>
<td>{{vehicle_list_load.Type}}</td>
<td>
<a href= "{% url 'vehicle_movement:checkoutview' pk = vehicle_list_load.pk %}" class = "glyphicon glyphicon-pencil" aria-hidden ="true" > Edit</a>
</td>
</tr>
{% endfor %}
this is vehicle_movements urls.py
from django.urls import path
from vehicle_movement import views
app_name = 'vehicle_movement'
urlpatterns = [
path('checkoutview/<int:pk>/',views.checkout, name = 'checkoutview'),
]
this is main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(('vehicle_movement.urls','vehicle_movement'),namespace = 'vehicle_movement')),
]
This is the view
def listView(request):
vehicle_list_loads = list(Checkin.objects.all().filter(Type ='Loading'))
vehicle_list_unloads = list(Checkin.objects.all().filter(Type ='Unloading'))
current_time = datetime.utcnow().replace(tzinfo=utc)
diff = current_time
return render(request,'vehicle_movement/listView.html',
{'vehicle_list_loads':vehicle_list_loads,'vehicle_list_unloads':vehicle_list_unloads,'diff':diff})
on clicking on edit this view needs to open
def checkout(request,pk):
post = get_object_or_404(Checkin, pk= pk)
return render(request,'vehicle_movement/checkout.html',{'post':post})
Your urls.py seems to be setup ok, I think the reason it is not working for you is because you have extra spaces around the parameter. The correct way, in the templates, to pass arguments is like this:
{% url 'vehicle_movement:checkoutview' pk=vehicle_list_load.pk %}
Related
I am doing a simple form site with Django. This is what my sites url is looks like: mysite.com/register/12345678
I want to print the part after the register (12345678) to the order id field. When someone goes this mysite.com/register/87654321 url then i want to print it.
How can i do that? These are my codes.(Currently using Django 1.11.10)
forms.py
from django import forms
from .models import Customer
from . import views
class CustomerForm(forms.ModelForm):
class Meta:
model = Customer
fields = (
'order_id','full_name','company','email',
'phone_number','note')
widgets = {
'order_id': forms.TextInput(attrs={'class':'orderidcls'}),
'full_name': forms.TextInput(attrs={'class':'fullnamecls'}),
'company': forms.TextInput(attrs={'class':'companycls'}),
'email': forms.TextInput(attrs={'class':'emailcls'}),
'phone_number': forms.TextInput(attrs={'class':'pncls'}),
'note': forms.Textarea(attrs={'class':'notecls'}),
}
views.py
from django.shortcuts import render
from olvapp.models import Customer
from olvapp.forms import CustomerForm
from django.views.generic import CreateView,TemplateView
def guaform(request,pk):
form = CustomerForm()
if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
form.save(commit=True)
else:
print('ERROR FORM INVALID')
theurl = request.get_full_path()
orderid = theurl[10:]
return render(request,'forms.py',{'form':form,'orderid':orderid})
customer_form.html
{% extends 'base.html' %}
{% block content %}
<h1>REGÄ°STRATÄ°ON</h1>
<form class="registclass" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-default">REGISTER</button>
</form>
{% endblock %}
urls.py
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^thanks/$',views.ThankView.as_view(),name='thank'),
url(r'^register/(?P<pk>\d+)',views.guaform,name='custform'),
]
You have passed the value to your view as 'pk' so you can use that to set the the initial value:
views.py
form = CustomerForm(initial={'order_id': pk})
SamSparx is right, here's some additional information to help prevent such errors in advance:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^thanks/$',views.ThankView.as_view(),name='thank'),
url(r'^register/(?P<pk>\d+)',views.guaform,name='custform'),
]
You are using regex to parse your path. Regex is generally speaking not recommended in this case according to the docs because it might introduce more errors and is harder to debug. (Unoptimized) RegEx also tends to be slower.
For simple use cases such as your path here, consider choosing the default pathing syntax as below:
urlpatterns = [
url('admin/', admin.site.urls),
url('thanks/',views.ThankView.as_view(),name='thank'),
url('register/<int:pk>',views.guaform,name='custform'),
]
You could of course also use string instead of int depending on your usage of pk.
Your paths do not all end with a slash consistently. This might impact your SEO and confuse users. See this and this.
Also your form is not imported .as_view() for some reason which could cause some problems.
Update :: Problem solved.just follow the guy below.
in my urls.py
path('', store_view, name='store'),
path('category/<str:category_name>/', category_view, name='category'),
in views.py
def store_view(request):
categories = list(Category.objects.all())
context = {
'categories': categories,
}
return render(request, 'store/store.html', context)
def category_view(request, category_name):
category = Category.objects.get(name=category_name)
context = {
'category': category,
}
return render(request, 'store/single-category-view.html', context)
in my template : store.html , that is rendered by store_view >>
{% for item in categories %}
<a href="{% url 'category' item.name %}">
{{item.name}}
</a>
{% endfor %}
Now,the problem is, in the category column in my DB, i have got one category called 'Laptop/MacBook'.when ever this name is passed to the url, it says >>
"Reverse for 'category' with arguments '('Laptop/MacBook',)' not
found. 1 pattern(s) tried: ['category/(?P<category_name>[^/]+)/$']
But when i changed the category name from Laptop/MacBook to Laptop and MacBook , it worked fine and showed no error.
But i want to keep it as it was,'Laptop/MacBook'.How can i do that??and how do you guys deal with that?
Try encoding and decoding your DB values. Assuming its Python 3:
from urllib.parse import quote, unquote
encoded = quote("Laptop/Macbook", safe="")
decoded = unquote(encoded)
print(encoded, decoded)
Output:
Laptop%2FMacbook Laptop/Macbook
With this your route should take in the right param.
from django.http import HttpResponse, request
from django.shortcuts import render
def store_view(request):
name = "Laptop/Macbook"
return render(request, './store.html', context={"name": name})
def category_view(request, category_name):
print(category_name)
return HttpResponse(b"Here we go!")
templatetags/tags.py
from urllib.parse import quote, unquote
from django import template
register = template.Library()
#register.filter(name='encode')
def encode(name):
return quote(name, safe="")
#register.filter(name='decode')
def decode(name):
return unquote(name)
Template:
{% load tags %}
<a href="{% url 'category' name|encode %}">
{{name}}
</a>
Don't forget to add in settings:
'OPTIONS': {
'libraries':{
'tags': 'templatetags.tags',
}
},
When using a "/", django thinks that you are passing more than one parameter. To fix this, replace str by path in your urls like so:
path('', store_view, name='store'),
path('category/<path:category_name>/', category_view, name='category'),
This will make django understand that the / does not mean there are two separate parameters in your url.
I have a html template on which multiple messages are posted and can be deleted using a 'delete' button that has been created. My code seeks to search for the id of the item to be deleted and delete it, redirecting to /deleteMessage and concatenating with the id number. I don't fully understand the process and have an error I cannot spot.
html form
<ul>
{% for g in all_items %}
<li> {{ g.content }}
<form action="/deleteMessage/{{g.id}}/"
style="display: inline;"
method="post">{% csrf_token %}
<input type="submit" value="Delete"/>
</form>
</li>
{% endfor %}
</ul>
views.py relevant code
def deleteMessage(request,GuestBookItem_id):
item_to_delete =GuestBookItem.objects.get(id=GuestBookItem_id)
item_to_delete.delete()
return HttpResponseRedirect('/worldguestbook/')
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('worldguestbook/',worldguestbookView),
path('login/',loginView),
path('addMessage/',addMessage),
path('deleteMessage/',deleteMessage),
]
I imagine it is this line that is causing the error - an error in concatenation and not redirecting to the right path.
**<form action="/deleteMessage/{{g.id}}/"**
Error message:
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/deleteMessage/17/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
worldguestbook/
login/
addMessage/
deleteMessage/
The current path, deleteMessage/17/, didn't match any of these.
What I tried:
I tried, in views.py changing this to g.id (instead of GuestBookItems.id) to match with what is in the form, but that didn't work either.
item_to_delete =GuestBookItem.objects.get(id=g_id)
You need to capture GuestBookItem_id in the URL pattern:
path('deleteMessage/<int:GuestBookItem_id>/', deleteMessage),
Note that in Python, you would normally use guest_book_item_id as the variable name. Or since it's the primary key of a model instance, you could use pk. It would be a good idea to use get_object_or_404, so that you get a 404 page when the item does not exist.
You're already using a POST request, which is a good idea when you are changing or deleting objects. You should also check that it's a POST request in the view.
Finally, it's a good idea to reverse URLs instead of hardcoding them. First, you need to add names to your URL patterns, then you can use {% url %} in the template and reverse() or the redirect shortcut in the template.
Putting that together, you get:
<form action="{% url 'delete_message' g.id %}">
urlpatterns = [
path('admin/', admin.site.urls),
path('worldguestbook/', worldguestbookView, name="worldguestbook"),
path('login/', loginView, name="login"),
path('addMessage/', addMessage, name="add_message"),
path('deleteMessage/', deleteMessage, name="delete_message"),
]
path('deleteMessage/<int:pk>/', deleteMessage),
from django.shortcuts import get_object_or_404, redirect
def deleteMessage(request, pk):
if request.method == "POST"
item_to_delete = get_object_or_404(GuestBookItem, pk=pk)
item_to_delete.delete()
return redirect('worldguestbook')
Template-html
<p>
Inscreva-se
</p>
URLS.py
urlpatterns = [
url(r'^$', index, name = 'index'),
#url(r'^(?P<pk>\d+)/$', details, name = 'details'),
url(r'^(?P<slug>[\w_-]+)/$', details, name = 'details'),
url(r'^(?P<slug>[\w_-]+)/inscricao/$', enrollment, name = 'enrollment'),
]
Url does not find the path specified.
You have a space between the % and } at the end of the tag.
{% url 'courses:enrollment' courses.slug % }
^
Change it to
{% url 'courses:enrollment' courses.slug %}
Since you are using the namespace courses in your tag courses:enrollment, you should set app_name in your courses/urls.py file.
app_name = 'courses'
urlpatterns = [
...
]
You are using the word "courses" in your URL reversal, even though it was never defined. Try using just this: {% url 'enrollment' courses.slug % }
How does {"% url 'news_id' %"} work?
I do have a url pattern url(r'^((?:\w|-)+)/$', 'comments.views.home', name='news_id') but I still get NoReverseMatch.
It says
Reverse for 'news_id' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['((?:\w|-)+)/$']
views.py
from django.shortcuts import render
from comments.models import User
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader, Context
from comments.models import News, User, Comment
from django.core.urlresolvers import resolve
def home(request, url_arg):
print "in Views.py", "and url_arg = ", url_arg
c = Comment.objects.all().filter(news__news_id=url_arg)
n = News.objects.all().get(news_id=url_arg)
cont = Context({'news': n.text, 'cts': c, 'news_id': n.news_id})
rc = RequestContext(request, cont)
t = loader.get_template('home.html')
print 'n = ', n
return HttpResponse(t.render(rc))
return render(request, 'home.html', context_dict, context_instance=RequestContext(request))
def submit(request):
print "request.path_info = ", request.path_info
print "in submit method and url = ", resolve(request.path_info).url_name, " & other try = ", request.resolver_match.url_name
news_id = request.POST.get('news_id')
user_id = request.POST.get('user_id')
comment_text = request.POST.get('comment')
print "news_id =", news_id, "user_id = ", user_id, "comment_text = ", comment_text
n = News(news_id=news_id)
n.save()
u = User(name='random',user_id=user_id)
u.save()
c = Comment(news=n, user=u, text=comment_text)
c.save()
return HttpResponse("Thanks!")
urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# Examples:
url(r'^(?:\w|-)+/submit/$','comments.views.submit'),
url(r'^((?:\w|-)+)/$', 'comments.views.home', name='news_id'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
]
home.html
<p>
News: {{news}}<br>
</p>
<form action="{% url news_id %}" method="post"> {% csrf_token %}
User Id: <input type="text" name="user_id"> <br>
Comment:<br>
<input type="text" name="comment" placeholder="Express your opinion ...">
<input type="submit" value="Submit"> <br>
{% for ct in cts %}
{{ ct.text }}<br>
{% endfor %}
</form>
You've misunderstood a few things here. In particular, you've mixed up the name of the URL pattern, and the parameters you need to pass to it.
For instance, if you wanted to capture the ID of a post in your blog and pass it to a detail view, you would normally do something like:
url(r'^(?P<post_id>)/$', views.post_detail, name='post_detail')
so the name of the pattern is post_detail and the parameter it takes is called post_id. So you would reverse it by doing:
{% url "post_detail" post_id=my_post.id %}
Also, your regex is very odd. You have a non-capturing group as the only contents of a capturing group. I can't tell what you are actually trying to do with that pattern, but normally a home page wouldn't take any parameters at all - and in fact you are not actually passing any parameters. Just use r'^$'.