I have looked around and can't really find a solution to my problem. Here is the error django throws. This error is being thrown when on my homepage I have a fiew links that upon clicking should direct you to a details view of said link.
Using the URLconf defined in untitled.urls, Django tried these URL patterns, in this order:
^$
^index/ ^$ [name='index']
^index/ ^(?P<distro_id>[0-9]+)/$ [name='distro_id']
^admin/
The current URL, index//, didn't match any of these.
To my knowledge I don't understand why this error is being thrown.
Here is my urls.py
from django.conf.urls import include, url
from django.contrib import admin
import index.views
urlpatterns = [
url(r'^$', index.views.index),
url(r'^index/', include('index.urls', namespace='index')),
url(r'^admin/', admin.site.urls),
]
My index/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# /index/
url(r'^$', views.index, name='index'),
#/distro/123/
url(r'^(?P<distro_id>[0-9]+)/$', views.detail, name='distro_id'),
]
My views.py
from django.shortcuts import get_object_or_404, render
from django.template import loader, RequestContext
from django.http import Http404
from .models import Distros
def index(request):
all_distros = Distros.objects.all()
context = {'all_distros': all_distros, }
return render(request, 'index/index.html', context)
def detail(request, distro_id,):
distro_id = get_object_or_404 (Distros, pk=distro_id)
return render(request, 'index/detail.html', {'distro_id': distro_id})
template code:
{% extends 'index/base.html' %}
{% block body %}
<ul>
{% for distro in all_distros %}
<li>{{ index.distro_id }}</li>
{% endfor %}
</ul>
{% endblock %}
I believe those are all the relevent files. I believe everything is setup correctly so I am not sure why the error is being thrown. I'm sure im missing something simple that i'm just overlooking.
Please don't use hardcoded URLs as they are error prone as in your situation. Instead of:
<a href="/index/{{ index.distro.id }}/">
use the url template tag with your namespace (index) and view name (distro_id):
<a href="{% url 'index:distro_id' index.id %}">
Note that you also have an error with index.distro.id as index is actually a Distros object. It has an id field, but not distro.id.
Related
I have a ran into a difficulty when navigating between different templates in a Django (v3.2) application. The app is called 'manage_remittance'.
The default landing page (which uses template manage_remittance/templates/manage_remittance/view_remittance.html) for the app should show a list of items (list is not relevant at the moment), and at the top of that list there should be a link, leading to another page in the same app, which would allow to add new items to the list.
The form that is invoked first is here:
manage_remittance/templates/manage_remittance/view_remittance.html
{% extends "root.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% url 'manage_remittance:remittance_add' as remittance_add %}
{% block title %}
VIEW REMITTANCES PAGE
{% endblock title %}
{% block content %}
<div class="list-group col-6">
Click here to add remittance data
</div>
I want to be able to get to another template (manage_remittance/templates/manage_remittance/remittance_add.html), but the {{ remittance_add }} has no value.
In addition, when I specify exact name of the html file (remittance_add.html) in the a href (see above), and click on it, I get a following error:
Using the URLconf defined in sanctions_project.urls, Django tried these URL patterns, in this order:
admin/
[name='login']
login/ [name='login']
logout/ [name='logout']
manage_remittance/ [name='view_remittance']
manage_remittance/ remittance_add/ [name='create_remittance']
^static/(?P<path>.*)$
^media/(?P<path>.*)$
The current path, manage_remittance/remittance_add.html, didn’t match any of these.
What am I doing wrong here?
fragment of urls.py for the project:
urlpatterns = [
path('admin/', admin.site.urls),
path('', login_view, name='login'),
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
path('manage_remittance/', include('manage_remittance.urls')), # namespace='manage_remittance'
]
urls.py at manage_remittance app:
from .views import (
CreateRemittanceInfo,
RemittanceListView
)
app_name = 'manage_remittance'
urlpatterns = [
path('', RemittanceListView.as_view(), name='view_remittance'),
path('remittance_add/', CreateRemittanceInfo.as_view(), name='create_remittance'),
]
views.py at manage_remittance app:
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.views.generic import ListView
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Remittance
class CreateRemittanceInfo(LoginRequiredMixin, CreateView):
model = Remittance
fields = ['remittance_text']
template_name_suffix = '_add'
class RemittanceListView(ListView):
model = Remittance
template_name = 'manage_remittance/view_remittance.html'
It seems that you have an issue in your urls:
manage_remittance/ remittance_add/ [name='create_remittance']
the name does not match with
manage_remittance:remittance_add
It seems quite simple: in your template, you should display a list, which is not the case. Have a look at Django User Guide: https://docs.djangoproject.com/fr/3.2/ref/class-based-views/generic-display/
It should look like
{% for object in object_list %}
<p>{{object.remittance_text}}</p>
{% endfor %}
I'm learning Chapter 18 18.4.2 in Python Crash Course,when i open http://localhost:8000/topics ,I'm using Django 3.0 and python 3.8
shows
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, topics, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
and this is my code
learning_log\urls.py
from django.contrib import admin
from django.urls import path
from learning_logs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name = 'index')
]
learning_logs\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name = 'index'),
url(r'^topics/$',views.topics,name='topics')
]
views.py
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
return render(request,'learning_logs/index.html')
def topics(request):
topics = Topic.objects.order_by('date_added')
context = {'topics':topics}
return render(request,'learning_logs/topics.html',context)
base.html
<p>
Learning Log-
Topics
</p>
{% block content %}{% endblock content %}
topics.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>{{ topic }}</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
and runserver shows:
Not Found: /topics
[06/Jan/2020 17:53:15] "GET /topics HTTP/1.1" 404 2077
enter image description here
First of all, good question. Love the detail.
The error is the following
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order
This means that the url /topics cannot be found. You need to import that url from the app specific urls.py into the main urls.py. This is usually done by something along the lines of
# in main urls.py
from learning_logs.urls import urlpatterns as ll_urlpatterns
# other patterns ...
urlpatterns += ll_urlpatterns
Use below code replace 'app' with your appname.
from django.contrib import admin
from django.urls import path
from learning_logs import views
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include(('app.urls', 'app'), namespace='app')),
]
Error:
NoReverseMatch at /
Reverse for 'detail' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$(?P<college_id>[0-9]+)/$']
The error is on this line:
<li>{{ college.college_name }}</li>
Here is the whole template (index.html):
{% if latest_college_list %}
<ul>
{% for college in latest_college_list %}
<li>{{ college.college_name }}</li>
{% endfor %}
</ul>
{% else %}
<p> No colleges available </p>
{% endif %}
The view:
from django.shortcuts import get_object_or_404, render
from .models import College
# Create your views here.
def index(request):
latest_college_list = College.objects.order_by('college_name')
context = {'latest_college_list': latest_college_list}
return render(request, 'app/index.html', context)
def detail(request, college_id):
college = get_object_or_404(College, pk=college_id)
return render(request, 'app/detail.html', {'college':college})
urls.py:
from django.conf.urls import url
from . import views
app_name = "app"
urlpatterns = [
# campusarchitecture.com/
url(r'^$', views.index, name="index"),
# /college_name
url(r'^(?P<college_id>[0-9]+)/$', views.detail, name="detail")
]
root urls conf:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('app.urls')),
url(r'^login/$', include('login.urls')),
url(r'^admin/', admin.site.urls),
]
Anyone know what the problem is?
Here is the issue:
url(r'^$', include('app.urls')),
Should be
url(r'^', include('app.urls', namespace="app")),
Note the $ should be removed as it indicates the end of the regex pattern, and it would not discover the included url patterns.
Secondly, you need to explicitly specify the namespace in the include. More on this in the documentation here.
Similarly, remove the $ after the login/ URL pattern match too.
So I am trying to get into Django by following the base tutorial but changing it a but to make into something I would actually use.
I have these urls.py:
from django.conf.urls import patterns, url
from budget import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^category/$', views.category, name='category'),
url(r'^category/(?P<category_id>\d+)/$', views.category_detail, name='category_details')
)
and
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^budget/', include('budget.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And then I have the following template:
{% if all_categories_list %}
<ul>
{% for category in all_categories_list %}
<li>{{ category.category_text }}</li>
{% endfor %}
</ul>
And this from views.py
def category(request):
all_categories_list = Budget_category.objects.order_by('category_type')
context = {'all_categories_list': all_categories_list}
return render(request, 'budget/category_list.html', context)
Now what I want to do is to remove the hardcoded /budget/category/ from the template and replace it with the url keyword to make it more flexible.
So change the line in the template to this
<li>{{ category.category_text }}</li>
But that gives an error of the type on localhost/budget/category/
NoReverseMatch at /budget/category/
Reverse for 'category' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['budget/category/$']
Now I know this has something to do with either namespaces or the regular expressions ( I think so at least) but I seem to have not enough insight yet to see the issue. Cause this link localhost/budget/category/1/ does work the way it should. So why won't it build that link properly?
The name of your url is category_details, not the category. So change the {% url %} tag to:
{% url 'category_details' category.id %}
I know that this is a common issues, but none of the answers which I found here helped me out.
I cannot figure out what is wrong here (Yes I tried with '' and without them in url)
Here's what I got so far
template:
<html>
<body>
<div> Link here </div> {{ formText }}
</body>
</html>
url(own config)
from django.conf.urls import patterns, include, url
from metadaten import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
root url:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^metadaten/', include('metadaten.urls', namespace='metadaten')),
url(r'^admin/', include(admin.site.urls)),
)
views:
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render, get_object_or_404
from metadaten.models import Title
from django.core.urlresolvers import reverse
def index(request):
return render(request, 'metadaten/index.html', {'formText' : 'foo'})
error message:
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
Any suggestions why I'm not able to build a simple href using {% url %} ?
please don't blame me if this question might be easy to figure out :(
You used a namespace with metadaten. You'll want to use {% url 'metadaten:index' %}.
Look at the last example for the url tag.