Django URL pattern not recognized due to tuple entry
I want to put a hyperlink in HTML code to redirect to a page with sending data with it. hyperlink path is dynamic so I used {% url "check" book.id %} in HTML code. book.id gives correct data but it appears in tuple format ('12',)
when I call particular page by writing static URL like http://127.0.0.1:8000/check/12/ it works fine "check" as view area and 12 as an argument passed
how to use a dynamic path
views.py
def add(request):
book = Readdata.objects.all()
return render(request, 'result.html', {'books': book})
def check(request,id):
return render(request,'result.html',{'result': id})
urls.py
urlpatterns = [
url('add', views.add, name='add'),
url(r'^check/(\d+)/$', views.check, name="delete_book"),
url('', views.index, name="index")
]
html
click not working
click working
error-
Reverse for 'check' with arguments '(21,)' not found. 1 pattern(s)
tried: ['check']
This has nothing to do with tuples.
As the error says, you don't have a "check" URL that takes an argument "21". The only URL that takes an argument is "delete_book". "check" takes no arguments.
Related
Django code is to create an Entry Page where Visiting /wiki/title, where TITLE is the title of an encyclopedia entry, should render a page that displays the contents of that encyclopedia entry. The view should get the content of the encyclopedia entry by calling the appropriate util function. If an entry is requested that does not exist, the user should be presented with an error page indicating that their requested page was not found.
But if statement is working fine, but else statement is not working. When I am browsing http://127.0.0.1:8000/wiki/CSS, it should navigate me to that dedicated page. But I am getting an error: markdown() missing 1 required positional argument: 'text'. Please advise how to mitigate this error?
views.py
from markdown import markdown
from django.shortcuts import render
from . import util
#EntryPage using title
def entry(request, entry):
#markdowner = markdown()
entryPage = util.get_entry(entry)
#Displays the requested entry page, if it exists
if entryPage is None:
return render(request, "encyclopedia/nonExistingEntry.html", {
"entryTitle": entry
})
# Title exists, convert md to HTML and return rendered template
else:
return render(request, "encyclopedia/entry.html", {
"entry": markdown().convert(entryPage),
"entryTitle": entry
})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="entry")
]
util.py
def get_entry(title):
"""
Retrieves an encyclopedia entry by its title. If no such
entry exists, the function returns None.
"""
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
As Kaleba said, try to change the line in your views.py to
"entry": markdown(entryPage)
I have a index.html page with a list of "cards", where each card have a "Click to Select" link.
When user click in this link i'd like to call a function in python to select this item, see:
def selectItem(request, item):
#so something with this item
so, in my html pagE:
<div class="card-action">
Selecionar
</div>
This don't work. What is the right way to do it ?
You can not call a function like that. A browser requests data with an HTTP request, and the server answers with an (HTTP) response. Such requests have a URL, and Django can route the request - with the URL - to the right view that will calculate a response.
We thus need to construct a view that can then be called. Your call is already quite close:
# app/views.py
from django.http import HttpResponse
def select_item(request, item_id):
# so something with this item_id
# ...
return HttpResponse()
Since most objects are not serializable (and usually you do not want that anyway, since it would expose a lot of (potentially sensitive) data to the user, we thus need to work with an id (an identifier that is for example stored in the database that corresponds to an object).
The response contains the data in the response. Frequently that is HTML code that is then rendered by the browser.
Now in urls.py, we can specify how the url looks like, for example:
# app/urls.py
from django.urls import path
from app.views import select_item
urlpatterns = [
path('select_item/<int:item_id>/', select_item, name='select_item_view'),
# ...
]
The urlpatterns need to be included in the root urlpatterns (in the project root).
Now in the HTML template, we can generate the URL that matches with this view, something similar to:
<div class="card-action">
Selecionar
</div>
Django will then make sure that the href points to an URL that refers to the select_item view with the correct parameter.
I'm suprised that I cannot access my product detail page through the url and I don't understand why since I've already done this basic thing plenty of times...
I have a page where all my products are displayed, when the user click on a product he is redirected to the product detail, that's it.
Somehow when I click a link linked to the product detail or type de correct path to the url it loads the same page where all the product are shown but it doesn't even call the product detail view, why so ?
Here are the views :
def rcdex(request):
list = Liste.objects.all()
return render(request, 'rcdex.html', {'list':list,})
def rc_detail(request, id):
list = Liste.objects.get(id=id)
return render(request, 'rc_detail.html', {'list':list,})
Here are the urls :
url(r'^', views.rcdex, name="rcdex"),
url(r'^rc/(?P<id>\d+)/$', views.rc_detail, name='rc_detail'),
Here is how I call the rc_detail view on the template :
<th>{{ l.entreprise }}</th>
I don't get why it doesn't show me the correct template (rc_detail.html) but instead reload rcdex.html ?
You haven't terminated your rcdex urlpattern, so it matches everything. You should use a $:
url(r'^$', views.rcdex, name="rcdex"),
you can also do like this..
url(r'^rc/(?P<id>\d+)/$', views.rc_detail, name='rc_detail'),
url(r'^', views.rcdex, name="rcdex"),
I am trying to redirect users to a create-profile form after signup.Since I don't know how to use dynamic url for LOGIN_REDIRECT_URL.I tried this little trick of linking it to a static view then finally to a dynamic view using redirect.It gives the error Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] (with 'badguy' truly the username of the just signed-up user').From the error,it is clearly passing the username to the view.My codes are:
settings.py
LOGIN_REDIRECT_URL = '/prof-change/gdyu734648dgey83y37gyyeyu8g/'
urls.py
url(r'^prof-change/gdyu734648dgey83y37gyyeyu8g/',views.login_redir,name='login_redir'),
url(r'^create-profile/(?P<username>\w+)/$',views.add_profile,name='create-profile'),
views.py
def login_redir(request):
user=get_object_or_404(User,username=request.user.username)
username=user.username
return redirect('add_profile',username=username)
def add_profile(request,username):
userman=User.objects.get(username=username)
........
........
When you reverse urls (including when you use a url name with the redirect shortcut), you should use the name of the url pattern, not the view. The url pattern has name='create-profile', so use that.
redirect('create-profile', username=username)
in views.py I have this:
def logout(request,key):
auth.logout(request)
return HttpResponseRedirect(reverse('airAgency.views.index',args=[key]))
in index template i wanna when user click on a link,logout view run:
logout
I wanna pass key parameter to logout view,Imagine I have an object named agn and it's WebSite field is going to pass to logout view, s.th like this:
<a href="{% url airAgency.views.logout agn.WebSite %}">
Imagine agn.WebSite has the value of mastane above code cause this error:
Caught NoReverseMatch while rendering: Reverse for 'airAgency.views.logout' with arguments '(u'mastane',)' and keyword arguments '{}' not found.
what's the right way to do this?
Caught NoReverseMatch - usually means that the url is being called by incorrect arguments, or that something else is wrong when {% url %} tries to call the url specified.
You need to define url pattern for that view function to be called in urls.py. And in the url pattern specify a view function.
urls.py
url(r'^logout/(?P<key>[a-zA-Z0-9\-]+)/$', "airAgency.views.logout", name="logout"),
index.html
<a href="{% url logout agn.WebSite %}">
This might not work if you just copy paste it because I don't know the exact project setup.
The key is to have urls.py where regex is used to create patterns for how the url looks like, then a view function that needs to be invoked, and finally a name, which you can then use in your templates.
This is all explained in the basic Django tutorial.