My Django site is working fine locally. I am pushing it to my website but one of the links is not working and I can not for the life of me see why as similar things are working fine.
Here is my urls.py:
from . import views
app_name = 'sitepages'
urlpatterns = [
path('', views.greeting_page_def, name='greeting_page_html'),
path('home/', views.home_def, name='home_html'),
path('specs/', views.specs_def, name='specs_html'),
path('about/', views.about_def, name='about_name'),
path('faq/', views.faq_def, name='faq_name'),
path('howTos/', views.howTos_def, name='howTos_name'),
path('howTos/puller', views.howTosPuller_def, name='howTosPuller_name'),
path('howTos/conformer', views.howTosConformer_def, name='howTosConformer_name'),
path('howTos/general', views.howTosGeneral_def, name='howTosGeneral_name'),
path('howTos/submitter', views.howTosSubmitter_def, name='howTosSubmitter_name'),
]
And my views.py:
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
# Create your views here.
def greeting_page_def(request):
return render(request, 'sitepages/greeting_page_html.html')
def about_def(request):
return render(request, 'sitepages/about.html')
def faq_def(request):
return render(request, 'sitepages/faq.html')
def home_def(request):
return render(request, 'sitepages/home.html')
def specs_def(request):
return render(request, 'sitepages/specs.html')
def howTos_def(request):
return render(request, 'sitepages/howTos.html')
def howTosPuller_def(request):
return render(request, 'sitepages/howTosPuller.html')
def howTosConformer_def(request):
return render(request, 'sitepages/howTosConformer.html')
def howTosGeneral_def(request):
return render(request, 'sitepages/howTosGeneral.html')
def howTosSubmitter_def(request):
return render(request, 'sitepages/howTosSubmitter.html')
and the html template that has the links.
{% extends 'base.html' %}
{% block content %}
{% load static %}
<a class="link-primary" href="{% url 'sitepages:howTosPuller_name' %}"} >How Tos - Puller</a>
<a class="link-primary" href="{% url 'sitepages:howTosSubmitter_name' %}"} >How Tos - Submitter</a>
<a class="link-primary" href="{% url 'sitepages:howTosGeneral_name' %}"} >How Tos - General</a>
<a class="link-primary" href="{% url 'sitepages:howTosConformer_name' %}"} >How Tos - Conformer</a>
{% endblock %}
all the links here work except for
<a class="link-primary" href="{% url 'sitepages:howTosGeneral_name' %}"} >How Tos - General</a>
when I click that I get
NoReverseMatch at /sitepages/howTos/
Reverse for 'howTosGeneral_name' not found. 'howTosGeneral_name' is not a valid view function or pattern name.
the other thing is it works locally but not on my site and I am pushing it via git and it looks the same.
Where am I going wrong?
All links have extra } at the end before closing of opening anchor tag.
So, the template should be:
{% extends 'base.html' %}
{% block content %}
{% load static %}
<a class="link-primary" href="{% url 'sitepages:howTosPuller_name' %}">How Tos - Puller</a>
<a class="link-primary" href="{% url 'sitepages:howTosSubmitter_name' %}">How Tos - Submitter</a>
<a class="link-primary" href="{% url 'sitepages:howTosGeneral_name' %}">How Tos - General</a>
<a class="link-primary" href="{% url 'sitepages:howTosConformer_name' %}">How Tos - Conformer</a>
{% endblock %}
Note: Always add / at the end of every route, so:
Urls.py:
from . import views
app_name = 'sitepages'
urlpatterns = [
path('', views.greeting_page_def, name='greeting_page_html'),
path('home/', views.home_def, name='home_html'),
path('specs/', views.specs_def, name='specs_html'),
path('about/', views.about_def, name='about_name'),
path('faq/', views.faq_def, name='faq_name'),
path('howTos/', views.howTos_def, name='howTos_name'),
path('howTos/puller/', views.howTosPuller_def, name='howTosPuller_name'),
path('howTos/conformer/', views.howTosConformer_def, name='howTosConformer_name'),
path('howTos/general/', views.howTosGeneral_def, name='howTosGeneral_name'),
path('howTos/submitter/', views.howTosSubmitter_def, name='howTosSubmitter_name'),
]
Related
Whenever i tried to view other people profile, it always return my own profile again, i don't really know what's wrong and i have tried calling request.user but it seems not to work
views.py
def UserProfile(request, username):
user = get_object_or_404(User, username=username)
profile = Profile.objects.get(user=user)
url_name = resolve(request.path).url_name
context = {
'profile':profile,
'url_name':url_name,
}
return render(request, 'userauths/profile.html', context)
urls.py main project
from userauths.views import UserProfile
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('userauths.urls')),
path('<username>/', UserProfile, name='profile'),
]
index.html
{% for creator in creators %}
<img class="avatar-img rounded-circle" src="{{creator.user.profile.image.url}}" alt="creators image" ></div>
<h5 class="card-title"><a href="{% url 'profile' creator.user %}">
{{creator.user.profile.first_name}} {{creator.user.profile.last_name}} {% if creator.verified %} <i class="bi bi-patch-check-fill text-info smsall" ></i> {% else %}{% endif %} </a></h5>
<p class="mb-2">{{creator.user.profile.bio}}</p></div>
{% endfor %}
I just fixed it now, in my profile.html, i am using {{request.user.profile.first_name}} to display the informations. I was supposed to use {{profile.first_name}} etc, and that was it.
Instead of this:
<a href="{% url 'profile' creator.user %}">
you should try:
<a href="{% url 'profile' creator.user.username %}">
From the presumably working statement get_object_or_404(User, username=username) I assume that username is a field (hopefully, unique) of the User. So you should pass it onto the URL, not the entire User model.
I have been facing this issue. And I have a url name post-page-detail but then also getting error please
See the error screenshot below.
My singlepost.html html page
<a href="{% url "post-detail-page" slug=post.slug %}">
<h2>{{post.tractor_company}} || {{post.tractor_model}}</h2>
<pre><h5>{{post.implimentaion}}</h5>
{{post.farmer}}
Uplode Date : {{post.date}}</pre>
</a>
</div>
URLs.py
from . import views
urlpatterns = [
path("",views.starting_page,name = "starting-page"),
path("posts",views.posts,name = "post-page"),
path("posts/<slug:slug>",views.post_detail,name="post-detail-page"),
]
View.py
from django import forms
from django.contrib.auth.models import User
from django.shortcuts import render, redirect ,get_object_or_404
from .models import Post, Farmer
# Create your views here.
from django.http import HttpResponseRedirect
# Create your views here.
def starting_page(request):
return render(request,"tractor/index.html")
def posts(request):
qs = Post.objects.all()
context = {"posts":qs}
return render(request,"tractor/all-post.html",context)
def add_post(request):
pass
def post_detail(request,slug):
indentified_post = get_object_or_404(Post,slug=slug)
return render(request,"blog/post-detail.html",{'post':indentified_post})
i am iterating through posts and using the post-detail.html page
all-post.html.
{% load static %}
{% block title %}
All Tractors
{% endblock %}
{% block content%}
<section id="all_events">
<br>
<h1 style="text-align:center;">All Tractors</h1>
<ul>
{% for post in posts %}
<br>
{% include "tractor/includes/singlePost.html" %}
{% endfor %}
</ul>
</section>
{% endblock %}
Root urls.py
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path("",include(("tractor.urls","tractor"))),
]
The 'tractor' part in the path('', include(('tractor.urls', 'tractor'))) in the include(…) function [Django-doc] specifies the app_namespace. This means that for all URLs you use in the tractor.urls urls, you should prefix this with tractor::
<a href="{% url 'tractor:post-detail-page' slug=post.slug %}">
…
</a>
I've been having this error for hours and I can't seem to fix it!
URL.PY:
from django.urls import path
from . import views
app_name = "learning_logg"
urlpatterns = [
#Home Page
path('', views.index, name="index"),
]
VIEWS.PY:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "index.html")
INDEX.HTML:
{% extends "learning_logs/base.html" %}
{% block content %}
<p> Learning Log </p>
<p> Learning Log helps you keep track of your learning, for any topic you're learning about gay </p>
{% endblock content %}
BASE.HTML:
<p>
<a> href = "{% url "index" %}"> Learning Log</a>
</p>
{% block content%} {% endblock content %}
I'm trying to make base.html the main framework and then putting index.html inside it, does anyone know how to fix the error?
It says the error is in line 2:
1
2 href = "{% url "index" %}"> Learning Log
3
4
5
6 {% block content%} {% endblock content %}
7
As you have specified app_name in your urls.py. You should do the same when referencing the url in the url template tag like {% url 'learning_logg:index' %}.
See the details in the documentation here.
when i run my code (its for a project) i get this error: "no such table: encyclopedia_article". The error appears to come from 9 line of views.py (obj = article.objects.get(id=1). here is the code:
views.py
from django.shortcuts import render
from django.http import HttpResponse
from . import util
import random
from .models import article
from .forms import ArticleForm
def index(request):
obj = article.objects.get(id=1) #THIS IS WHAT APPERS TO CAUSE THE ERROR
context = {
'object': obj
}
entries = util.list_entries()
random_page = random.choice(entries)
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries(),
"random_page": random_page,
})
def CSS(request):
return render(request, "encyclopedia/css_tem.html", {
"article_css": "css is slug and cat"
})
def Python(request):
return render(request, "encyclopedia/python_tem.html", {
"article_python": "python says repost if scav"
})
def HTML(request):
return render(request, "encyclopedia/HTML_tem.html", {
"article_HTML": "game theory: scavs are future humans"
})
def Git(request):
return render(request, "encyclopedia/Git_tem.html", {
"article_Git": "github is git"
})
def Django(request):
return render(request, "encyclopedia/Django_tem.html", {
"article_Django": "this is a framework"
})
def new_article(request):
form = ArticleForm(request.POST or None)
if form.is_valid():
form.save()
context = {
'form': form
}
return render(request, "encyclopedia/new_article_tem.html", context)
models.py:
class article(models.Model):
title = models.CharField(max_length = 120)
description = models.TextField(blank=True, null = False)
forms.py:
from django import forms
from .models import article
class ArticleForm(forms.ModelForm):
class Meta:
model = article
fields = [
'title',
'description'
]
urls:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("", views.CSS, name="CSS"),
path("", views.Python, name="Python"),
path("", views.HTML, name="HTML"),
path("", views.Git, name="Git"),
path("", views.Django, name="Django"),
path("", views.new_article, name="new_article")
]
second urls (in other directory (there is encyclopedia and wiki this one is in wiki the last one in encyclopedia)):
"""wiki URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from encyclopedia import views
from encyclopedia.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("encyclopedia.urls")),
path('CSS/', views.CSS, name="CSS"),
path('Python/', views.Python, name="Python"),
path('HTML/', views.HTML, name="HTML"),
path('Git/', views.Git, name="Git"),
path('Django/', views.Django, name="Django"),
path('new_article/', views.new_article, name="new_article"),
path('main/', index)
]
new_article_tem.html:
{% extends "encyclopedia/layout.html" %}
{% block title %}
New Article
{% endblock %}
{% block body %}
<h1>Create Article</h1>
<form method = 'POST' action = "{% url 'index' %}"> {% csrf_token %}
<input type = 'submit' value='Save'>
</form>
{% endblock %}
IM NOT SURE IF THIS ONES ARE USEFULL BUT STILL IM PUTTING ALMOST EVERYTHING:
index.html:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<h1 id="demo" onclick="add_article()">Add article</h1>
<ul>
{% for entry in entries %}
<li><a href = /{{entry}}>{{ entry }}</a></li>
{% endfor %}
{% for article_title in created_articles %}
<li>{{article_title}}</li>
{% endfor %}
<li>{{title}}</li>
</ul>
{% endblock %}
layout.html:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="sidebar col-lg-2 col-md-3">
<h2>Wiki</h2>
<form>
<input class="search" type="text" name="q" placeholder="Search Encyclopedia">
</form>
<div>
Home
</div>
<div>
<a href = "/new_article" >Create New Article</a>
</div>
<div>
Random Page
</div>
{% block nav %}
{% endblock %}
</div>
<div class="main col-lg-10 col-md-9">
{% block body %}
{% endblock %}
</div>
</div>
</body>
</html>
Try to use this :-
python manage.py makemigrations
and then
python manage.py migrate
I need help with {% url (url to template) %} when i try to open another view i see a NoRewerseMatch error.
here is my html file:
{% load static %}
<head>
<link href='{% static "navbar_style.css" %}' rel="stylesheet", type="text/css">
</head>
<header>
<nav class="headlist">
--->
<ul>
<li>O nas</li>
<li>Kontakt</li>
<li><a>Zajęcia</a></li>
</ul>
</nav>
</header>
my app(pages)/urls.py
from django.contrib import admin
from django.urls import path
from . import views
app_name = 'pages'
urlpatterns = [
path('', views.home_view, name='home_view'),
path('index/', views.index_view, name='index_view'),
]
views.py
import...
# Create your views here.
def home_view(request):
listoftexts = Text.objects.order_by('-pub_date')
context = {
'listoftexts': listoftexts
}
return render(request, 'pages/home.html', context)
def index_view(request):
listoftexts = Text.objects.order_by('-pub_date')
context = {
'listoftexts': listoftexts
}
return render(request, 'pages/index.html', context)
You can do it like this.
The name of the view is home_view:
path('', views.home_view, name='home_view'),
so you can use the {% url … %} template tag [Django-doc] with:
<a href="{% url 'home_view' %}">
If you use an app_name in the urls.py, you also need to prefix this in the url:
<a href="{% url 'pages:home_view' %}">
The url_name should be home_view not home. The name parameter in the path function is what you use to refernce the view.