I'm making a Django website that has Full Text Search with Postgres. However, when I make my search, none of the results show up. Is there anything I did wrong? Here's my code:
Views.py
from django.shortcuts import render
from django.contrib.postgres.search import SearchVector, SearchQuery, SearchRank
from django.views.generic import ListView, FormView
from .models import Post
from .forms import *
# Create your views here.
class HomepageView(FormView):
template_name = 'selling/home.html'
form_class = SearchForm
class Search(ListView):
model = Post
template_name = 'selling/search.html'
def get_queryset(self):
query = self.request.GET.get('q')
posts = Post.objects.annotate(
search=SearchVector('title', 'description'),
).filter(search=SearchQuery(query))
return posts
Models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
book_author = models.CharField(max_length=30)
phone_contact = models.CharField(max_length=20)
price = models.DecimalField(max_digits=7,decimal_places=2)
postal_code = models.CharField(max_length=20)
def __str__(self):
return self.title
Forms.py
from django import forms
class SearchForm(forms.Form):
q = forms.CharField(label='Search', max_length=50)
Search.html
{% extends "selling/base.html" %}
{% load static %}
{% block content %}
<div class="container pt-5 pb-5">
<div class="ui divided items">
{% for item in posts %}
<div class="item">
<div class="image">
<img src="{% static 'selling/images/brebeuf.png' %}">
</div>
<div class="content item-container">
<a class="header item-title">{{ item.title }} </a><b class="item-price-right">${{ item.price }}</b>
<div class="meta">
<span>{{ item.postal_code }} | {{ item.date_posted|date:"d/m/Y" }}</span>
</div>
<div class="description item-description">
<p>{{ item.description }}</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock content %}
home.html
{% extends "selling/base.html" %}
{% load static %}
{% block content %}
<form action="{% url 'search' %}" method='get'>
{{ form }}
</form>
{% endblock content %}
settings.py - installed apps
INSTALLED_APPS = [
'selling.apps.SellingConfig',
'users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.postgres',
]
urls.py
from django.urls import path
from .views import Search, HomepageView
urlpatterns = [
path('', HomepageView.as_view(), name='sell-home'),
path('search/', Search.as_view(), name='search'),
]
That's all the code. Feel free to ask if you need anything else.
Related
So i have a project called star social project this project is similar to a socail media that you can post and create group but this project you can only post when you are in a group. So i get an error message that is not familiar to me which is on the title, i tried to search on google and get some result but when i implement it to my project it does not work. So why im getting this error is because i'm trying to create a comment section and when i click the add comment that's when i get the error message. So i'm here to ask someone to help me because i'm not really familiar on this error and i'm just learning django for about 2 months now.
models.py
##########################
## POSTS MODELS.PY FILE ##
##########################
from django.contrib.auth import get_user_model
from django.db import models
from groups.models import Group
from misaka import html
from django.urls import reverse
from django.utils import timezone
User = get_user_model()
class Post(models.Model):
user = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
group = models.ForeignKey(Group, related_name='posts', null=True, blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.message
def save(self, *args, **kwargs):
self.message_html = html(self.message)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse(
'posts:single',
kwargs={
'username': self.user.username,
'pk': self.pk
}
)
class Meta:
ordering = ['-created_at']
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
username = models.CharField(max_length=50)
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
approved = models.BooleanField(default=False)
def __str__(self):
return self.text
def approved(self):
self.approved = True
self.save()
views.py
class AddComment(LoginRequiredMixin, generic.CreateView):
model = Comment
fields = ('username', 'text')
def form_valid(self, form):
post = get_object_or_404(Post, pk=self.kwargs.get('pk'))
self.object = form.save(commit=False)
self.object.post = post
self.object.save()
return super().form_valid(form)
urls.py
########################
## POSTS URLS.PY FILE ##
########################
from django.urls import path
from posts import views
app_name = 'posts'
urlpatterns = [
path('', views.PostList.as_view(), name='all'),
path('by/<username>/', views.UserPost.as_view(), name='for_user'),
path('by/<username>/<int:pk>/', views.PostDetail.as_view(), name='single'),
path('new/', views.CreatePost.as_view(), name='create'),
path('delete/<int:pk>/', views.DeletePost.as_view(), name='delete'),
path('post/comment/', views.AddComment.as_view(), name='add_comment'),
]
comment_form.html
{% extends 'posts/post_base.html' %}
{% load bootstrap4 %}
{% block prepost %}
<h4>Add New Comment</h4>
<form id="commentForm" action="{% url 'posts:add_comment' %}" method="POST">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-large btn-primary">Add Comment</button>
{% endbuttons %}
</form>
{% endblock %}
_post.html
<div class="media">
<h3 class="mr-5">#{{ post.user.username }}</h3>
<div class="media-body">
<strong>{{ post.user.username }}</strong>
<h5>{{ post.message_html|safe }}
<time class="time">{{ post.created_at }}</time>
{% if post.group %}
<span class="group-name">in {{ post.group.name }}</span>
{% endif %}
</h5>
<hr>
{% if user.is_authenticated %}
<a class="btn btn-primary btn-comment" href="{% url 'posts:add_comment' %}">Add Comment</a>
{% endif %}
<div class="container">
{% for comment in comment_list %}
<br>
{{ comment.created }}
<p>{{ comment.text|safe|linebreaks }}</p>
<p>Posted By: <strong>{{ comment.username }}</strong></p>
{% endfor %}
</div>
<div class="media-footer">
{% if user.is_authenticated and post.user == user and not hide_delete %}
<a class="btn btn-simple" href="{% url 'posts:delete' pk=post.pk %}" title="delete"><span class="fa fa-remove text-danger" aria-hidden="true"></span><span class="text-danger icon-label">Delete</span></a>
{% endif %}
</div>
</div>
</div>
main urls.py
from django.contrib import admin
from django.urls import path, include
from sm_project import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.HomePage.as_view(), name='home'),
path('loginpage/', views.LoginPage.as_view(), name='loginpage'),
path('thankspage/', views.ThanksPage.as_view(), name='thankspage'),
path('accounts/', include('accounts.urls', namespace='accounts')),
path('accounts/', include('django.contrib.auth.urls')),
path('groups/', include('groups.urls', namespace='groups')),
path('posts/', include('posts.urls', namespace='posts')),
]
First you used incorrect action in your comment form i think it should be action="{% url 'posts:AddComment' %}" instead of action="{% url 'posts:add_comment' %}"
and second you are using get_object_or_404() which returns 404 page if object is not found so try to to remove it for debugging you will get exact error.
I am new to django and creating one app. I have create two mode on is master and other is details.
While using a bootstrap nav bar drop down on a single base.html i am able to get master records in drop down while i switch to different page when clicked any drop down element i didn't get drop down element. I am extending base.html in both the master as well as detail page. Please help with any option
dropdown working
http://127.0.0.1:8000/environment/
dropdown not working
http://127.0.0.1:8000/environment/env_name
base.html
<!-- menu buttons -->
<div class="collapse navbar-collapse" id="topNavBar">
<ul class="nav navbar-nav">
<li class="active">
<li class="nav-item dropdown" >
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-globe" aria-hidden="true"></span> Environment
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{% for envmaster in all_envmaster %}
<a class="dropdown-item" href="{% url 'environment:detail' envmaster.env_name %}">{{ envmaster.env_name }}</a>
<div class="dropdown-divider"></div>
{% endfor %}
</div>
<!--<a href="{% url 'environment:index' %}">
<span class="glyphicon glyphicon-globe" aria-hidden="true"></span> Environment
</a>-->
</li>
</li>
<li class="">
<a href="#">
<span class="glyphicon glyphicon-file" aria-hidden="true"></span> Details
</a>
</li>
</ul>
detail.html
<!-- Loads the path to your static files -->
{% extends 'environment/base.html' %}
{% block title %}Environment Details{% endblock %}
{% block body %}
<!--<img src="{{ envmaster.env_name }}" style="width: 250px;"><br>-->
<h3>{{ envmaster.env_name }}</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
{% csrf_token %}
{% for envdetail in envmaster.envdetail_set.all %}
<li>{{ envdetail.env_component }} - {{ envdetail.component_class }} - {{ envdetail.value_1 }}</li><br>
{% endfor %}
{% endblock %}
index.html
<!-- Loads the path to your static files -->
{% extends 'environment/base.html' %}
{% block title %}Environment Details{% endblock %}
{% block body %}
{% if all_envmaster %}
<h3>Here are all Environment:</h3>
<ul>
{% for envmaster in all_envmaster %}
<li>{{ envmaster.env_name }}</li>
{% endfor %}
</ul>
{% else %}
<h3>You don't have any Environment</h3>
{% endif %}
{% endblock %}
models.py
from django.db import models
from django.urls import reverse
class Envmaster(models.Model):
env_name = models.CharField(primary_key=True, max_length=256)
env_type = models.CharField(max_length=256, blank=True, null=True)
env_owner = models.CharField(max_length=256, blank=True, null=True)
cur_release = models.CharField(max_length=256, blank=True, null=True)
class Meta:
db_table = 'ENVMASTER'
def get_absolute_url(self):
return reverse('environment:detail', kwargs={'pk': self.pk})
def __str__(self):
return self.env_name
class Envdetail(models.Model):
env_name = models.ForeignKey(Envmaster,on_delete=models.CASCADE)
env_component = models.CharField(max_length=256, blank=True, null=True)
component_class = models.CharField(max_length=256, blank=True, null=True)
value_1 = models.CharField(max_length=256, blank=True, null=True)
value_2 = models.CharField(max_length=256, blank=True, null=True)
value_3 = models.CharField(max_length=256, blank=True, null=True)
additional_info = models.CharField(max_length=256, blank=True, null=True)
class Meta:
db_table = 'ENVDETAIL'
def __str__(self):
return self.env_component
views.py
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.shortcuts import render, get_object_or_404
from .models import Envmaster
class IndexView(generic.ListView):
template_name = 'environment/index.html'
context_object_name = 'all_envmaster'
def get_queryset(self):
return Envmaster.objects.all()
class DetailView(generic.DetailView):
model = Envmaster
template_name = 'environment/detail.html'
url.py
urlpatterns = [
re_path(r'^$', views.IndexView.as_view(), name="index"),
#/environment/env_name/
re_path(r'^(?P<pk>[\w\-]+)/$', views.DetailView.as_view(), name='detail'),
You need to create a templates folder inside src folder you created and add create base.html inside that folder.
Than add in in you setting.py inside [Templates] add the location of the templates folder like this
'DIRS': [os.path.join(BASE_DIR, 'templates')],
and than just extend you html file
{% extends 'base.html' %}
While going through different posts and trying I have created a context file at app level so that once the class object is initialized it should retain the values within the navigation bar
from .models import Envmaster
def show_env_menu(context):
env_menu = Envmaster.onjects.all()
return {'env_menu': env_menu}
`I'm working on the code in chapter 1 of the django by example. Followed all instructions and reviewed many feedbacks on this site and other areas but not to my luck. I had earlier attempted get_absolute_url and return reverse method unsuccessfully. I was trying an instructed approach and land exactly the same issues in listview and detailview. I'm a beginner so I guess I'm missing something may be fundamental. Is there any version dependency. I've installed latest django and python. Need advice and thanks for any help!
This is the error message
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish_date')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = models.TextField()
publish_date = models.DateTimeField(default=timezone.now)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager() # default Manager
published = PublishedManager() # our custom manager
class Meta:
ordering = ['-publish_date',]
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse(
'blog:post_detail',
args=[
self.publish_date.year,
self.publish_date.month,
self.publish_date.day,
self.slug,
]
)
views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.published.all()
return render(request, 'blog/post/post_list.html', {'posts':posts})
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish_date__year=year,
publish_date__month=month,
publish_date__day=day)
return render(request, 'blog/post/post_detail.html',{'post':post})
urls.py ( For app)
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
#post views
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int=day>/<slug=post>/',
views.post_detail,
name='post_detail'),
]
admin.py
from django.contrib import admin
from .models import Post
#admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish_date', 'status')
list_filter = ('status', 'created_date','publish_date', 'author')
search_fields = ('title', 'body')
prepopulated_fields = {'slug':('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish_date'
ordering = ('status', 'publish_date')
post_detail.html
{% extends "blog/base.html" %}
{% block title %}{{post.title}}{% endblock %}
{% block content %}
<h> {{post.title}} </h>
<p class="date">
Published {{ post.publish_date }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{%endblock%}
post_list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{% url 'blog:post_list' post.title %}"
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish_date }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
Modified post_list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
{% url 'blog:post_detail' post.publish_date.year post.publish_date.month post.publish_date.day post.publish_date.slug as post.title %}
<a href="{{ post.title }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish_date }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
in your post_list.html
change this
<a href="{% url 'blog:post_list' post.title %}"
to
<a href="{% url 'blog:post_list'%}"
your url doesn't accept an argument but you are passing an argument from your template.
I am following "django by example"
and i met the problem but i don't know what causes it.
The following is the error page:
NoReverseMatch at /blog/
Reverse for 'post_detail' with arguments '('', '', '')' not found. 1 pattern(s) tried: ['blog/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/$']
Request Method:
GET
Request URL:
http://127.0.0.1:8000/blog/
Django Version:
1.11
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'post_detail' with arguments '('', '', '')' not found. 1 pattern(s) tried: ['blog/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/$']
Exception Location:
E:\workspace\pycharm\djangobyexample\mysite\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable:
E:\workspace\pycharm\djangobyexample\mysite\env\Scripts\python.exe
Python Version:
3.5.2
Python Path:
['E:\workspace\pycharm\djangobyexample\mysite',
'E:\workspace\pycharm\djangobyexample\mysite\env\Scripts\python35.zip',
'E:\workspace\pycharm\djangobyexample\mysite\env\DLLs',
'E:\workspace\pycharm\djangobyexample\mysite\env\lib',
'E:\workspace\pycharm\djangobyexample\mysite\env\Scripts',
'c:\users\richard\appdata\local\programs\python\python35\Lib',
'c:\users\richard\appdata\local\programs\python\python35\DLLs',
'E:\workspace\pycharm\djangobyexample\mysite\env',
'E:\workspace\pycharm\djangobyexample\mysite\env\lib\site-packages']
Main URLConfiguration
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),
]
blog/url.py
from django.conf.urls import url
from . import views
urlpatterns = [
# post views
# url(r'^$', views.post_list, name='post_list'),
url(r'^$', views.PostListView.as_view(), name='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',
views.post_detail,
name='post_detail'),
#url(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'),
]
views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from .forms import EmailPostForm
from django.core.mail import send_mail
# Create your views here.
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'blog/post/detail.html', {'post': post})
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class PublishedManager(models.Manager):
def get_query(self):
return super(PublishedManager, self).get_query().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = {
('draft', 'Draft'),
('published', 'Published'),
}
title = models.CharField(max_length=250, primary_key=True)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, related_name='blog_post')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
class Meta:
# Telling django to sort results by the publish field in descending order by default when we query the database
ordering = ('-publish',)
def __str__(self):
return self.title
objects = models.Manager()
published = PublishedManager()
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
detail.html
{% extends "blog/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html " with page=page_obj %}
{% endblock %}
base.html
{% load staticfiles %}
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>My blog</h2>
<p>This is my blog.</p>
</div>
</body>
</html>
This line is giving you the error, because the arguments are invalid.
<!-- <a href="{% url 'blog:post_detail' post.year post.month post.day %}">-->
The post does not have year, month and day attributes -- they are attributes of post.publish.
You are already using {{ post.get_absolute_url }} on the next line of the template to get the url. Since the url tag line is inside an html comment <!-- -->, the easiest fix is to simply delete the line.
I made a slug field and I made migrations. My slug field is called slug and it is visible in in the site administration, but it doesn't work properly.
My browser tells me:
TypeError at /None/
detail() got an unexpected keyword argument 'slug'
models.py
from django.db import models
class Question(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(null=True, blank=True)
description = models.CharField(max_length=200)
data_published = models.DateTimeField('date published')
large_description = models.TextField(max_length=20000)
def __str__(self):
return self.title
def save(self):
if not self.id:
self.s = slugify(self.q)
super(test, self).save()
def was_published_recently(self):
return self.datum_objave >= timezone.now() - datetime.timedelta(days=1)
urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'books.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<slug>[\w_-]+)/$', 'books.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
index.html
{% extends "master.html" %}
{% block h1 %}
<div class="box first">
<div class="row">
<div class="container">
{% for question in latest_question_list %}
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="center">
<h4>{{ question.title }} </h4>
<p>{{ question.description }}</p>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
{% block title %} Index {% endblock %}
admin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
question_fields = {"slug": ("title",)}
admin.site.register(Question, QuestionAdmin)
views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list': latest_question_list}
return render(request, 'papers/index.html', context)
def detail(request, book_title):
question = Question.objects.get(title=book_title)
return render(request, 'books/detail.html', {'question': question})`
detail.html
{% extends "master2.html" %}
{% block h1 %}
<div class="center">
<h4>{{question.title}} </h4>
<p>{{question.large_description}}</p>
</div>
{% endblock %}
{% block title %} Detail {% endblock %}
This part of your urls.py, (?P<slug>[\w_-]+), means that there is a view that accepts a parameter named slug and if Django is unable to find that view, it will complain. So your view should probably be something like this:
def detail(request, slug):
question = Question.objects.get(slug=slug)
return render(request, 'papers/detail.html', {'question': question})
UPDATE
It's also a good idea not to create your urls manually in your templates:
{{ question.naslov }}