Django passing data through the url, not finding page in url patterns - django

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>

Related

Cannot get url parameter to work in Django

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

Django using URL parameters in template

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')
]

Search controller is fetching the wrong template in Django

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 = [
# &downarrow; 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'),
]

Reverse for 's_note' with arguments '()' and keyword arguments '{'note_t': 'note_1 opeth', 'user_name': 'opeth'}' not found. 0 pattern(s) tried: []

I have a link to note detail page (s_note) in the user page (username). So as long as I have no entries(notes) in the database for the user the user page renders fine, but as soon as there is a valid note the render fails with the above error and points to ln:6 of user.html.
my urls.py
from django.conf.urls import url
from notes.models import User, Note
from . import views
app_name = 'notes'
urlpatterns = [
url(r'^$', views.index, name='u_index'),
my url
url(r'^signup/$', views.signup, name='u_signup'),
url(r'^(?P<user_id>[\w\-]+)/$', views.user, name='username'),
url(r'^(?P<user_name>[\w\-]+)/(?P<note_t>[\w\-]+)/$', views.note, name='s_note'),
url(r'^(?P<user_name>[\w\-]+)/(?P<note_t>[\w\-]+)/$', views.note, name='s_note')
]
my views
def note(request, user_name, note_t):
nt = Note.objects.get(note_title=note_t)
return render (request, 'notes/detail.html', {'note': nt})
my users.html
<h2>Hi! {{ user.user_n }} Your notes are here.</h2>
{% if allnotes %}
<ul>
{% for note in allnotes %}
<li>{{ note.note_title }}</li>
{% endfor %}
</ul>
{% else %}
<p>You have no notes yet!</p>
{% endif %}
<form method="post" action"">
<table>
{% csrf_token %}
{{ NForm }}
</table>
<input type="submit" value="Create">
</form>
Your url doesn't match for underscores or spaces which your keyword currently contains.
url(r'^(?P<user_name>[\w\-]+)/(?P<note_t>[\w\-]+)/$', views.note, name='s_note'),
should be
url(r'^(?P<user_name>[\w\-]+)/(?P<note_t>[\w\-\_\s]+)/$', views.note, name='s_note'),
although this isn't much of a solution since most spaces would turn into %20's, you should try to remove any spaces from your keywords and update your regex accordingly.
It was a namespacing problem as #Alasdair observed, it sorted with the edit -
'{% url 'notes:s_note'...%}'
in the template.

Django: Next value is changing oddly in login page

I wanted to redirect the users in my app to their users page if they were already logged in and tried to go directly to "../login/". I've found this answer:
Django: Redirect logged in users from login page
It works fantastic until I decide to hit the "Registration" link I have below my login fields. I don't know why but when I hit it, I get redirect to the login page again but the only thing that changes is the url, for some reason it becomes "http://localhost:8000/users/login/?next=/users/register/", and it wont take me to my registration page.
Why the "next" variable changes if I've set it with another url in the login template like so:
{% extends "base.html" %}
{% block title %}User Login{% endblock %}
{% block head %}User Login{% endblock %}
{% block content %}
{% if form.errors %}
<p>User name or password is incorrect.</p>
{% endif %}
<form method="post" action="{% url login %}">
{% csrf_token %}
<p><label for="id_username">Username:</label>
{{ form.username }}</p>
<p><label for="id_password">Password:</label>
{{ form.password }}</p>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="users/"/>
</form>
<li>Register</li>
{% endblock %}
I'm using django1.4 and python 2.7. My urls.py are:
For the whole application:
urlpatterns = patterns('',
url(r'^$', main_page, name="main_page"),
url(r'^users/',include('user_manager.urls')),
)
For the user_manager module:
urlpatterns = patterns('user_manager.views',
url(r'^$', users, name="user_page"),
url(r'^logout/$', user_logout, name="logout"),
url(r'^login/$', user_login, name="login"),
url(r'^(\w+)/$', user_page),
url(r'^register/$', register_page),
)
Do you have a #login_required decorator on that register_page view? If so, remove it
Ok I've found the problem. Django checks the urls regex in order so the "register" url was matching the "(\w+)/" intended to work for the users pages. So all I had to do is put that url at the end, the urls.py now looks like this:
urlpatterns = patterns('user_manager.views',
url(r'^$', users, name="user_page"),
url(r'^logout/$', user_logout, name="logout"),
url(r'^login/$', user_login, name="login"),
url(r'^register/$', register_page),
url(r'^(\w+)/$', user_page),
)
Nevertheless I still don't understand quite well why that mistaken match was changing the next value. I know that caused it but I don't know why...
Thank you very much for your time!