no such table: (Django) - python

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

Related

NoReverseMatch at /search/ Reverse for 'entry' not found. 'entry' is not a valid view function or pattern name

NoReverseMatch at /search/
Reverse for 'entry' not found. 'entry' is not a valid view function or pattern name.
I´m trying to make an search engine for my website in django, via views.py, but Django always says that there's an exception in views.py
Views.py
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entries(request, entry):
if entry not in util.list_entries():
raise Http404
content = util.get_entry(entry)
return render(request,"encyclopedia/entry.html",
{"title": entry, "content": Markdown().convert(content)},
)
def search(request):
query = request.GET.get("q", "")
if query is None or query == "":
return render(
request,
"encyclopedia/search.html",
{"found_entries": "", "query": query},
)
entries = util.list_entries()
found_entries = [
valid_entry
for valid_entry in entries
if query.lower() in valid_entry.lower()
]
if len(found_entries) == 1:
return redirect("entry", found_entries[0])
return render(
request,
"encyclopedia/search.html",
{"found_entries": found_entries, "query": query},
)
But Django says: "if len(found_entries) == 1:
return redirect("entry", found_entries[0])
have an "NoReverseMatch" error"
Urls.py
from django.urls import path
from . import views, util
urlpatterns = [
path("", views.index, name="index"),
path("entries/<str:entry>", views.entries, name="entries/{{ entry }}"),
path("search/", views.search, name="search"),
]
handler404 = 'encyclopedia.views.error_404_view'
Layout.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<body>
<div class="row">
<div class="sidebar col-lg-2 col-md-3">
<h2>Wiki</h2>
<form action="{% url 'search' %}">
<input class="search" type="text" name="q" placeholder="Search
Encyclopedia">
</form>
<div>
Home
</div>
<div>
Create New Page
</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>
I've been trying a lot of things, but nothing fix it, how I can let my page run well?
You must change your url name in urls.py to entry:
path("entries/<str:entry>", views.entries, name="entry"),
And you should pass the argument in your views.py file as:
return redirect("entry", entry=found_entries[0])
See https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#redirect

How do I fix this Getting no reverse match error in django

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>

Ref doesn't work correctly with my settings

I'm learning to make some chat website, and i cannot quite get how to use urls correctly. The problem is that when I get to "http://127.0.0.1:8000/chat", links into the header became like "http://127.0.0.1:8000/chat/main" instead of "http://127.0.0.1:8000/main"
project urls:
from django.contrib import admin
from django.urls import path
from django.urls import include, path
from chat import views as chat_views
from mainapp import views as mainapp_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', mainapp_views.index),
path('chat/', include('chat.urls')),
]
chat app urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<str:room_name>/', views.room, name='room'),
]
chat app views:
from django.shortcuts import render
def index(request):
return render(request, 'chat/chatindex.html')
def room(request, room_name):
return render(request, 'chat/room.html', {
'room_name': room_name
})
index.html
<!DOCTYPE html>
{% load static %}
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
<title>{% block title %}{% endblock title %}</title>
</head>
<body>
<header>
<div class="navbar navbar-dark bg-dark shadow-sm">
Главная
Поиск собеседников
Контакты
FAQ
Регистрация/Вход
</div>
</div>
</header>
<main>
<div>{% block content%}{% endblock content %}</div>
</main>
<footer class="footer mt-auto py-3 bg-light">
<div class="container">
<span class="text-muted">Давай общаться!</span>
</div>
</footer>
</body>
</html>
chatindex.html
{% extends "index.html" %}
{% block title %}Контакты{% endblock title %}
{% block header %}{% endblock header %}
{% block content %}
В какую комнату хотите зайти?
<br>
<input id="room-name-input" type="text" size="100">
<br>
<input id="room-name-submit" type="button" value="Enter">
<script>
document.querySelector('#room-name-input').focus();
document.querySelector('#room-name-input').onkeyup = function(e) {
if (e.keyCode === 13) { // enter, return
document.querySelector('#room-name-submit').click();
}
};
document.querySelector('#room-name-submit').onclick = function(e) {
var roomName = document.querySelector('#room-name-input').value;
window.location.pathname = '/chat/' + roomName + '/';
};
</script>
{% endblock content%}
I'm pretty sure that the problem is about "path('<str:room_name>/', views.room, name='room')", but i can't understand how to fix it and save opportunity to enter rooms with chat-page.
Thanks a lot!
Very likely you make relative paths. Indeed, if you visit a page like some.domain.com/foo, and the link is link, then it will visit some.domain.com/foo/bar, since that is a relative path: relative to the current path.
You can solve this by specifying an absolute path, which has a leading slash, so:
<!-- &downarrow; leading slash -->
FAQ
But it is better not to generate URLs manually. If you later for example change the URL paths, you will need to alter all URLs. You can give your views a name, like:
path('faq', views.faq, name='faq')
and in the template work with the {% url … %} template tag [Django-doc]:
<!-- &downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow;&downarrow; url template tag -->
FAQ
Django will then look for a view with the given name, and automatically generate the corresponding URL.

Django: My Register_Lost page not showing up

Still newbie in django, please help me
Urls.py :
from django.conf.urls import url
from . import views
app_name = 'lostfound'
urlpatterns = [
# /lostfound/
url(r'^$', views.IndexView.as_view(), name='index'),
# /lostfound/lostitems
url(r'lostitems/', views.LostItemsView.as_view(), name='lost_items'),
this is the main problem of my url, i can't display my register_lost.html
# /lostfound/lostitems/addlostitems
url(r'/addlostitems/$', views.RegisterLostView.as_view(), name='register_lost'),`
Views.py :
from django.views import generic
from .models import Wallet
class IndexView(generic.ListView):
model = Wallet
template_name = 'lostfound/index.html'
class LostItemsView(generic.ListView):
model = Wallet
template_name = 'lostfound/lost_items.html'
class RegisterLostView(generic.ListView):
model = Wallet
template_name = 'lostfound/register_lost.html'
lost_items.html :
<div class="menu-button z-depth-3 right">
<a class="waves-effect waves-light btn" href="{% url 'lostfound:register_lost' %}">
<i class="medium material-icons right">playlist_add</i>Tambah Data
</a>
</div>
this is my register_lost.html that i want process
register_lost.html :
{% extends 'lostfound/base.html' %}
{% extends 'lostfound/lost_items.html' %}
{% block title %}Register Lost Items{% endblock %}
{% block content %}
{% load staticfiles %}
{% block sign_in %}
{% endblock %}
<h1>Hello</h1>
{% endblock %}
Try to switch lines in the urlpatterns list
this:
url(r'/addlostitems/$', views.RegisterLostView.as_view(), name='register_lost'),
before this:
url(r'lostitems/', views.LostItemsView.as_view(), name='lost_items'),

Unexpected NoReverseMatch error when using include() in urls patterns

I'm getting an error when referencing detail.html in index.html
Reverse for 'detail' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$(?P<pk>[0-9]+)/$']
views.py
def rock_and_feat(request):
feats = Feat.objects.order_by('-created')[:3]
rocks = Rockinfo.objects.order_by('-rank')[:50]
context = RequestContext(request, {
'feats': feats, 'rocks': rocks
})
return render_to_response('template.html', context)
class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'
context_object_name = 'feat'
urls.py
urlpatterns = [
url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
index.html
{% extends "index.html" %}
{% block mainmast %}
<div id="wrapper">
{% if feats %}
{% for feat in feats %}
<div class="specialsticky">
<img src="{{ feat.image.url }}" alt="some text">
<h1 class="mast-header">
{{feat.title}}
</h1>
</div>
{% endfor %}
{% else %}
<p>No </p>
{% endif %}
</div>
{% endblock %}
detail.html
{% extends "index.html" %}
<iframe width="560" height="345" src="{{ feat.youtube_link }}" frameborder="0" allowfullscreen></iframe>
project urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', include('feature.urls', namespace="feature")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the app worked fine before I added the <a href= on the image in index.html.
Can't figure out what's wrong.
This indicates the problem.
'$(?P<pk>[0-9]+)/$'
There shouldn't be a dollar sign (which matches the end of the string) at the beginning of the pattern.
The problem is caused by the way you are including the urls.py. You currently have a dollar in the regex:
url(r'^$', include('feature.urls', namespace="feature")),
To fix the problem, remove the dollar from the regex.
url(r'^', include('feature.urls', namespace="feature")),

Categories