django template just reloads when clicking a link pointing to another template - python

the template home in my django contains a url pointing to another page but when clicking this link , the home template just reloads here is the code for the urls.py, views.py and home.html
this is urls.py
from django.conf.urls import include, url
from django.contrib import admin
from pizza import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'',views.home,name='home'),
url(r'order/',views.order,name='order'),
]
this is views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'pizza/home.html')
def order(request):
return render(request,'pizza/order.html')
this is home.html
<h3>
Nandias Garder
</h3>
order a pizza

your urls formatting is wrong. you should try this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$',views.home,name='home'),
url(r'^order/',views.order,name='order'),
]
if you notice here I used ^$ to make sure the home url only catches the empty url.
in your urls, the second url (home url) will match with every string. you can go to admin, because it's defined before home url.

Related

Supposed simple Django slug categories, but I get a 404 and I can't figure out why

thanks for taking a moment to try and help me out. Haven't done much coding in many years, switched careers for some reason.. Decided to play with Django, everything was going fine, and I can't figure out this URL/slug problem. I've got some links, first.
It's explained on their GitHub rep but I can't figure it out. I've also used this handy site and am still in the dark.
I Know its relatively simple, just having a hard time finding up to date code and books, and I've been frustrating myself on it for too long to quit. It's got to be an error with my URL's, I just don't know what though. So, have a laugh at my expense (seriously, you should have heard Linode support when I logged into the wrong account and deleted the wrong server.. they have excellent support!) and help me amuse myself a bit through this pandemic, because I'm mostly playing with stufff as part of a bigger project I started a while before I quit computer science and went into welding..
Basically, add a page to a category. Better looking URL's with slugs, no problem, until I try to access outside of /admin with the slug. I've basically got it text for text, and am getting this:
Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:
404
[name='index']
rango/
admin/
category/slug:category_name_slug/ [name='category']
^media/(?P.*)$
The current path, category/useless/, didn't match any of these.
I get that when I try to access localhost/rango/category/category-name-slug, or localhost/category/category-name-slug
Here is my project urls.py
from django.contrib import admin
from django.urls import path
from django.urls import include
from django.conf import settings
from django.conf.urls.static import static
from rango import views
urlpatterns = [
path('', views.index, name='index'),
path('rango/', include('rango.urls')),
path('admin/', admin.site.urls),
path('category/slug:category_name_slug/', views.category, name='category'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And this is from my app urls.py
from django.urls import path
from rango import views
app_name='rango'
urlpatterns = [
path('', views.index, name='index'),
path('category/slug:category_name_slug/', views.category, name='category'),
]
And a snippet from my index.html file, because I can't get category.html to load anywhere without a 404, I can get the slug name to show up beside the category name, if you're wondering what on earth I'm doing.
{% if categories %}
<ul>
{% for category in categories %}
<li>;{{ category.name }} {{ category.slug }};
</li>
{% endfor %}
</ul>
{% else %}
{% endif %}
and last, my app's views.py
from django.shortcuts import render
from rango.models import Category
from rango.models import Page
def index(request):
category_list = Category.objects.order_by( '-likes' )[:50]
context_dict = { 'categories': category_list }
return render(request, 'rango/index.html', context_dict)
def category(request, category_name_slug):
context_dict = {}
category = Category.objects.get(slug=category_name_slug)
context_dict['category_name'] = category.name
pages = Page.objects.filter(category=category)
context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
return render(request, 'rango/category.html', context_dict)
I get that when I try to access localhost/rango/category/category-name-slug, or localhost/category/category-name-slug
There is a rendering problem with the Markdown file. If you inspect the raw version [GitHub], you see the real urlpatterns. This needs to use angle brackets (<…>), so:
urlpatterns = [
path('', views.index, name='index'),
path('rango/', include('rango.urls')),
path('admin/', admin.site.urls),
path('category/<slug:category_name_slug>/', views.category, name='category'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#SanZo- Why are you adding the following urlpattern at two places.
path('category/slug:category_name_slug/', views.category, name='category'),
It should only be inside your app (rango).
I don't understand why you would try this.rango/ admin/ category/slug:category_name_slug/
The simplest thing to do is - if you want to access the category_slug view
then do this 127.0.0.1:8000/rango/category/{slug}/
e.g 127.0.0.1/rango/category/pillow/

NoReverseMatch - not a registered namespace

I am new to Django and I was working at my first project, but I got:
NoReverseMatch with Exception Value: 'hiring_log_app' is not a
registered namespace in base.html
which follows:
href="{% url 'hiring_log_app:index' %}" >Hiring Log
href="{% url 'hiring_log_app:topics' %}" >Topics
I made sure my namespace was correct and I looked at the other topics already opened without figuring out how to solve the issue.
I paste urls.py:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include ('hiring_log_app.urls', namespace='hiring_log_app')),
hiring_log_app/urls.py:
"""Define URL patterns for hiring_log_app"""
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
def index(request):
"""The home page for hiring_log_app"""
return render(request, 'hiring_log_app/index.html')
def topics(request):
""" list of topics"""
topics = Topic.objects.raw( "SELECT text FROM HIRING_LOG_APP_TOPIC;")
context = {'topics' : topics}
return render(request, 'hiring_log_app/topics.html', context)
Does anyone know where I am making a mistake?
You have wrongly specified the namespace in urlpatterns. Follow the pattern below:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
Edit:
If you are using Django 2.2.4 then you should use path instead of url since the usage of it is replaced by re_path.
from django.contrib import admin
from django.conf.urls import include
from django.urls import re_path
urlpatterns = [
re_path(r'^admin/', include(admin.site.urls)),
re_path(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))

What does 'Reverse' means in errors when running Django?

I'm trying to run a blog build with django on the browser. And I got this error:
NoReverseMatch at /
Reverse for 'blog.views.post_detail' not found.
'blog.views.post_detail' is not a valid view function or pattern name.
My url.py of my app looks like:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
]
It seems that when I type 127.0.0.1:8000/.
The url will direct to views.post_list.
And my views.py looks like:
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.filter(published_date__isnull=False)
return render(request, 'blog/post_list.html', {'posts': posts}
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
post_list() will render the request with post_list.html.
Inside post_list.html, the error comes from the line:
<h1>{{ post.title }}</h1>
I don't really understand what 'Reverse' means in the error message. 'blog.views.post_detail' does exist in views.py. I think I got everything needed for the code and can't figure out what went wrong.
I'm new to django, sorry if the question is basic and thanks for answering!
Django 1.10 removed the ability to reverse urls by the view's dotted import path. Instead, you need to name your url pattern and use that name to reverse the url:
urlpatterns = [
url(r'^$', views.post_list, name='post-list'),
url(r'^(?P<pk>\d+)/$', views.post_detail, name='post-detail'),
]
And in your template:
<h1>{{ post.title }}</h1>
It seems that your urls.py should be as follows:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^(?P<pk>\d+)/$', views.post_detail),
]
You should define a name for your url:
urlpatterns [
url(r'^$', views.post_list,name=post_list),
]
then use url tag like this:
AppName is you django application name.

NoReverseMatch error after upgrade Django version

I'm newbie in Django and i read many topic here and not found solution for my case. I believe it's easy, but i can't find the solution.
Basically i have the code in my urls.py and the works fine in Django 1.8.4:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'controli2c.view.view_home'),
]
My template file contains
{% url 'controli2c.views.view_home' as home_url%}
<a href="{% url 'controli2c.views.view_home' %}" {% if request.path == home_url %} class="active"{% endif %} >HOME</a>
When i update Django, i get the error "TypeError: view must be a callable or a list/tuple in the case of include()". Then, i change my urls.py code to:
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'views.view_home'),
]
Now, i have the NoReverseMatch when i open the browser (http://localhost:8000):
"Reverse for 'controli2c.view.view_home' not found. 'controli2c.views.view_home' is not a valid view function or pattern name."
In a post in the forum i found:
"The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py. If your URL patterns don't have names, then now is a good time to add one, because reversing with the dotted python path no longer works."
I believe that's my problem. But i don't know what changes i have to do.
Anyone can help me?
Thanks a lot!!
Now you have to pass a callable, so:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', views.view_home),
]
I think it might work now.
I found the solution!
To keep my template file with the same code, i have make these change
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'views.view_home',name='controli2c.views.view_home'),
]
Thanks!

Django NoReverseMatch error, reverse function not work with no arguments

this NoReverseMatch error is driving me nuts. I'm using Django 1.6 and I have checked through my urls and it just doesn't work. Please kindly guide me on this.
I basically want to do something deadly simple, just when I submit a html form, I get the data I want and then redirect to a result page, but it just doesn't work...
Here is my index.html file
<form name="input" action="{% url 'whatspring:sending' %}" method="post">
{% csrf_token %}
Recipient: <input type="text" name="usrname">
<br>
<input type="submit">
</form>
<br>
my view.py
def index(request):
return render(request,'springsend/index.html')
def sending(request):
var = request.POST['usrname']
doSomethinghere()
return HttpResponseRedirect(reverse('whatspring:results'))
def results(request):
return render(request,'springsend/send_results.html')
then my app urls.py
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
)
and the main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^send/', include('springsend.urls', namespace="whatspring")),
)
I have tried to look into the problem and it seems that the reverse function cannot get the name for the reverse url that I want (i.e. 'results' under the namespace 'whatspring'....)Am I missing something something trival? Please kindly help.
Your urls.py (springsend one) doesn't seem to have a url for the sending view, that's probably why {% url 'whatspring:sending' %} can't find it.
Simply change it to
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
url(r'^sending/$', views.sending, name='sending'), # this line
)
Every accessible view needs a url. The user's browser needs to have some address to send things. If it would just send it to your domain without url, Django would have no way to tell which url is requested. Django does not auto-generate these urls (which is probably good).
(The user himself does not need to know this url; you don't need to place any ` links anywhere.)

Categories

Resources