I need to redirect to another page with same url parameters so I don't understand how to use url parameters in current template to redirect.
dborg.html:
{% extends './frame.html' %}
{% block content %}
Создать нового пользователя
{% csrf_token %}
{{form}}
{% for i in o %}
<p><a href='{% url "view_user" i.id %}'>{{i.fld}}</a></p>
{% endfor %}
{% endblock %}
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.main, name='main'),
path('view_org/<str:orgtype>/<str:deg>',
views.view_org_db, name='view_org'),
path('token/<str:orgtype>/<str:deg>', views.get_token, name='token'),
path('token/<str:orgtype>', views.get_token, name='token'),
path('create/<str:orgtype>/<str:deg>',
views.create_user, name='create_user'),
path('search/', views.search_user, name='search_user'),
path('report/', views.view_report, name='view_report'),
path('view/<int:id>', views.view_user, name='view_user')
]
Related
I am trying to pass a parameter through the url in Django, but nothing seems to be working.
This is my views.py:
from django.shortcuts import render
def show_user_profile(request, user_id):
assert isinstance(request, HttpRequest)
return render(request, "app/show_user_profile.html", {'user_id': user_id})
This is currently my urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('profile/', views.profile, name='profile'),
path(r'^show_user_profile/(?P<user_id>\w+)/$', views.show_user_profile, name="show_user_profile"),
path('admin/', admin.site.urls),
]
I've tried
http://localhost:50572/show_user_profile/aaa
http://localhost:50572/show_user_profile/aaa/
http://localhost:50572/show_user_profile/aaa//
http://localhost:50572/show_user_profile/?user_id=aaa
but I always get that same screen saying it can't find the url pattern.
But I've tried, all failed.
And neither does this:
path('show_user_profile/<int:user_id>/$', views.show_user_profile, name='show_user_profile')
This doesn't work either, by the way.
path(r'^show_user_profile/$', views.show_user_profile, name="show_user_profile"),
I've looked at the answers here and here, and I seem to be doing everything right. What am I missing?
EDIT:
Here's my show user profile template:
{% extends "app/layout.html" %}
{% block content %}
{% if request.session.uid %}
<div id="profile">
<div>
<span id="profile_prof_pic_content">
<img id="prof_pic" src="{{ user_data.prof_pic }}" width="100" height="100" />
</span>
<span>
{{ user_data.first_name }} {{ user_data.last_name }}
</span>
</div>
<div>
{{ user_data.prof_desc }}
</div>
</div>
{% else %}
<h2>You are not signed in. Log in to access this user profile.</h2>
{% endif %}
{% endblock %}
{% block scripts %}
{% load static %}
<script src="{% static 'app/scripts/jquery.validate.min.js' %}"></script>
Instead of this:
path(r'^show_user_profile/(?P<user_id>\w+)/$', views.show_user_profile, name="show_user_profile"),
Try this:
re_path(r'^show_user_profile/(?P<user_id>\w+)/$', views.show_user_profile, name="show_user_profile"),
And try to navigate on browser
Note: I have used re_path instead of path. you can check here
I'm trying to pass an object's id through the url, but its not able to find the page even after it tries the path when it goes through its patterns
Error:
Using the URLconf defined in GroomingService.urls, Django tried these URL patterns, in this order:
admin/
[name='Home']
appointment-Maker/
account/
admin-home
view_appointment/<id>/
login/ [name='Login Page']
registration/ [name='Registration Page']
logout [name='Logout Page']
The current path, adminview_appointment/21/, didn’t match any of these.
GroomingService.urls
#urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', Home_view, name="Home"),
path('appointment-Maker/', include('appointmentApp.urls')),
path('account/', include('accountApp.urls')),
path('admin-home', include('adminApp.urls')),
path('view_appointment/<id>/', viewAppointment_view), #this is the page it is not finding
path('login/', Login_view, name='Login Page'),
path('registration/', Registration_view, name='Registration Page'),
path('logout', Logout_view, name='Logout Page')
]
adminApp/views.py viewAppointment_view
def viewAppointment_view(request, id):
appointments = Appointment.objects.get(id = id)
context = {
'appointments' : appointments
}
return render(request, "admin_templates/viewappointment.html", context)
templates/admin_templates viewappointment.html
{% extends 'base.html' %}
{% block content %}
<a>appointment view</a>
{% endblock %}
templates/admin_templates adminhome.html (the link is clicked through this page)
{% extends 'base.html' %}
{% block content %}
<a>this is the admin page</a>
<br>
{% for a in appointments %}
Client name:{{a.client_dog_name}}<br> {% comment %} this is the link that is clicked {% endcomment %}
{% endfor %}
<br>
<a>find month</a>
<form method="POST">
{% csrf_token %}
{{ monthyear }}
<button class="btn btn-primary" type="submit">click</buttom>
</form>
{% endblock %}
If I'm missing anything please let me know, I had the path at the adminApp/urls.py earlier
urlpatterns = [
path('', views.adminhome_view, name="Admin Home"),
]
but moved it to where it was trying to find the urls. I have no idea why this might not be working.
It should be view_appointment/{{a.id}}, not adminview_appointment/{{a.id}}, but it is better to make use of the {% url … %} template tag [Django-doc]. You can give the view a name:
path('view_appointment/<int:id>/', viewAppointment_view, name='view_appointment'),
and then refer to it in the template:
Client name:{{a.client_dog_name}}<br>
I have problems to link my custom button from change_form.html to my view function.
pic : admin change form custom button
change_form.html
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block title %} Send Email {% endblock %}
{% block content %}
{% if request.resolver_match.url_name == 'requests_requests_change' %}
<div class="submit-row">
{% csrf_token %}
<a href="{% url 'requests:send-email' original.pk %}"
class="button" style="background-color: #F08000;float: left">Request Subscription
</a>
</div>
{% endif %}
{{ block.super }}
{% endblock %}
views.py
from django.shortcuts import render, redirect
def send_email(request, requests_id):
return redirect('') # or whatever to test the url
urls.py
from django.urls import path
from . import views
app_name = 'requests'
urlpatterns = [
path('<int:pk>/send-email/', views.send_email, name='send-email'),
]
main 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
urlpatterns = [
path('', admin.site.urls), # admin site administration
path('requests/', include('requests.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
pic: error message
Any help would be great! Thanks!
I have found the solution on my own.
The issue was the url inside the template.
Worked with this one:
Template :
<a href="{% url 'admin:requests_requests_change' original.pk %}send-email/"
And the URL path:
path('<int:pk>/change/send-email/', views.send_email, name='send-email')
I'm trying to write the controller to search for articles. But the search does not find anything and a template appears that is not specified in the views.py.
# views.py
class SearchView(ListView):
template_name = 'search_view.html'
def get_queryset(self):
query = self.request.GET.get('q')
object_list = Article.objects.filter(Q(title__icontains=query))
return object_list
# urls.py
urlpatterns = [
path('', ArticlesList.as_view(), name='list_view'),
path('<tag>/', ArticlesByTagsList.as_view(), name='articles_by_tags'),
path('articles/<slug:slug>', ArticleDetail.as_view(), name='detail_view'),
path('articles/create/', ArticleCreate.as_view(), name='create_view'),
path('articles/<slug:slug>/', ArticleUpdate.as_view(), name='update_view'),
path('articles/<slug:slug>/delete/', ArticleDelete.as_view(), name='delete_view'),
path('search/', SearchView.as_view(), name='search_view'),
]
#search_view.html
{% extends 'layout/basic.html' %}
{% block content %}
{{ object_list }}
{% endblock %}
The form looks like this
<form action="{% url 'articles:search_view' %}" method="get">
<input type="text" name="q" placeholder="Search...">
</form>
What am I doing wrong?
You should enumerate over the objects, so:
{% extends 'layout/basic.html' %}
{% block content %}
{% for object in object_list %}
{{ object.title }}
{% endfor %}
{% endblock %}
You should also specify the search/ path before the <tag>/ path, since Django always takes the item that first matches, and if you write search/ then it would first match with the <tag>/ and thus not fire the SearchView.
The urlpatterns thus should look like:
urlpatterns = [
# ↓ first specify the search/ path
path('search/', SearchView.as_view(), name='search_view'),
path('<tag>/', ArticlesByTagsList.as_view(), name='articles_by_tags'),
]
an effect of this, is that you can not use search as a tag. If you should be able to visit the articles_by_tags with search as tag, you should define non-overlapping patterns, so:
urlpatterns = [
path('search/', SearchView.as_view(), name='search_view'),
path('tag/<tag>/', ArticlesByTagsList.as_view(), name='articles_by_tags'),
]
I am getting an error saying:
NoReverseMatch at /books/
Reverse for 'urlvar' not found. 'urlvar' is not a valid
view function or pattern name.
I guess the {% with %} tag is not working well in books/index.html
but I don't know how to solve this.
this is my code:
books/urls.py
from django.conf.urls import url
from books import views
urlpatterns = [
url(r'^$', views.BooksModelView.as_view(), name='index'),
url(r'^book/$', views.BookList.as_view(), name='book_list'),
url(r'^author/$', views.AuthorList.as_view(), name='author_list'),
url(r'^publisher/$', views.PublisherList.as_view(), name='publisher_list'),
url(r'^book/(?P<pk>\d+)/$', views.BookDetail.as_view(), name='book_detail'),
url(r'^author/(?P<pk>\d+)/$', views.AuthorDetail.as_view(), name='author_detail'),
url(r'^publisher/(?P<pk>\d+)/$', views.PublisherDetail.as_view(), name='publisher_detail'),
]
templates/books/index.html
{% extends 'base_books.html' %}
{% block content %}
<h2>Books Management Systemt</h2>
<ul>
{% for modelname in object_list %}
{% with 'books:'|add:modelname|lower|add:'_list' as urlvar %}
<li>{{ modelname }}</li>
{% endwith %}
{% endfor %}
</ul>
{% endblock %}
urlvar is a variable, so you use urlvar in the {% url %} tag instead of the string 'urlvar'.
<li>{{ modelname }}</li>
You can construct the URL pattern name in the tag if you prefer, or keep the with tag if you find that more readable.
<li>{{ modelname }}</li>
Since you are using the 'books: namespace when you reverse the urls, you should set app_name in your books/urls.py file.
from django.conf.urls import url
from books import views
app_name = 'books'
urlpatterns = [
...
]