Not sure how to see my search results for Django-haystack? - python

I recently set up django and haystack and I have no errors but How do I see my search results? It made no mention of setting up a view for it only a template and a url. The action in the form template is this
<form method="get" action=".">
that didn't work so I changed it to this
<form method="get" action="posts/search">
and I also tried this with the slash after search
<form method="get" action="posts/search/">
I also tried to give my url a name
url(r'^search/', include('haystack.urls'), name='search'),
and have the form like this
<form method="GET" action="{% url 'posts:search' %}">
when I tried it like that it gave me this error
django.core.urlresolvers.NoReverseMatch: Reverse for 'search' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
how do I link my app properly to server so that the results display properly
heres my code
my form in my nav. the action is a page I had created before haystack the ones above are the new ones
<form method="GET" action="{% url 'posts:search-page' %}" class="navbar-form navbar-right" role="search">
<div>
<div class="input-group">
<input type="text" class="form-control" name="q" id="search" placeholder="search" value="{{ request.GET.q }}">
<span class="input-group-btn">
<button type="submit" class="btn btn-default">search</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</form>
my posts/urls.py
from django.conf.urls import url, include
from .views import post_create, post_detail, post_list, post_update, post_delete, post_search, tag_list
urlpatterns = [
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create, name='create'),
url(r'^search_results/$', post_search, name='search-page'),
url(r'^tag/(?P<slug>[\w-]+)/$', tag_list, name="tag_list"),
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<id>\d+)/delete/$', post_delete, name='delete'),
url(r'^search/', include('haystack.urls')),
]
my searche_indexes.py
from haystack import indexes
from .models import Post
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
publish = indexes.DateTimeField(model_attr='publish')
# content_auto = indexes.EdgeNgramField(model_attr='title')
def get_model(self):
return Post
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
my templates/search/search.html
{% extends 'base.html' %}
{% block content %}
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
{{ result.object.title }}
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}{% endif %}« Previous{% if page.has_previous %}{% endif %}
|
{% if page.has_next %}{% endif %}Next »{% if page.has_next %}{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% endblock %}
my post_text.txt
{{ object.title }}
{{ object.body }}
my Post model
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=120)
slug = models.SlugField(max_length=200, unique=True)
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
content = models.TextField()
draft = models.BooleanField(default=False)
publish = models.DateField(auto_now=False, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
tags = models.ManyToManyField(Tag)
objects = PostManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("posts:detail", kwargs={"slug": self.slug})
class Meta:
ordering = ["-timestamp", "-updated"]
as I said I got no system errors. This Is my 4th time attempting to use this. And Now there are no errors, Just some issue that has to do with displaying my results. Any guidance in the right direction is welcome

Related

Cannot resolve keyword 'slug' into field

Im making comment and reply system in my blog using Django. Now im trying to get queryset of comments that dont have reply comments(if I dont do this, reply comments will be displayed on a page as regular comments). Here is error that i got:
FieldError at /post/fourh-news
Cannot resolve keyword 'slug' into field. Choices are: comm_to_repl, comm_to_repl_id, comment_content, created, id, post, post_of_comment, post_of_comment_id, replies, user_created_comment, user_created_comment_id
Request Method: GET
Request URL: http://127.0.0.1:8000/post/fourh-news
Django Version: 4.1.2
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'slug' into field. Choices are: comm_to_repl, comm_to_repl_id, comment_content, created, id, post, post_of_comment, post_of_comment_id, replies, user_created_comment, user_created_comment_id
Exception Location: D:\pythonProject28django_pet_project\venv\lib\site-packages\django\db\models\sql\query.py, line 1709, in names_to_path
Raised during: blog.views.ShowSingleNews
Python Version: 3.10.4
Model:
class Post(models.Model):
title = models.CharField(max_length=150, verbose_name='Название')
slug = models.CharField(max_length=100, unique=True, verbose_name='Url slug')
content = models.TextField(verbose_name='Контент')
created_at = models.DateTimeField(auto_now=True, verbose_name='Дата добавления')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Дата обновления')
posted_by = models.CharField(max_length=100, verbose_name='Кем добавлено')
photo = models.ImageField(upload_to='photos/%Y/%m/%d', verbose_name='Фото', blank=True)
views = models.IntegerField(default=0)
category = models.ForeignKey('Category', on_delete=models.PROTECT, verbose_name='Категория')
tag = models.ManyToManyField('Tags', verbose_name='Тэг', blank=True)
comment = models.ForeignKey('Comment', verbose_name='Комментарий', on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('single_news', kwargs={'slug': self.slug})
class Meta:
ordering = ['-created_at']
class Category(models.Model):
title = models.CharField(max_length=150, verbose_name='Название')
slug = models.CharField(max_length=100, unique=True, verbose_name='category_url_slug')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('category', kwargs={'slug': self.slug})
class Meta:
ordering = ['title']
class Tags(models.Model):
title = models.CharField(max_length=150, verbose_name='Название')
slug = models.CharField(max_length=100, unique=True, verbose_name='tag_url_slug')
def get_absolute_url(self):
return reverse('news_by_tag', kwargs={'slug': self.slug})
def __str__(self):
return self.title
class Meta:
ordering = ['title']
class Comment(models.Model):
user_created_comment = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE, null=True)
post_of_comment = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE, null=True)
comment_content = models.TextField(verbose_name='Текст комментария')
created = models.DateTimeField(auto_now=True)
comm_to_repl = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, related_name='replies')
def get_absolute_url(self):
return reverse('single_news', kwargs={'slug': self.post_of_comment.slug})
class Meta:
ordering = ['-created']
View:
class ShowSingleNews(DetailView):
model = Post
template_name = 'blog/single_news.html'
context_object_name = 'post'
raise_exception = True
form = CommentForm
def post(self, request, *args, **kwargs):
form = CommentForm(request.POST)
if form.is_valid():
post = self.get_object()
form.instance.user_created_comment = request.user
form.instance.post_of_comment = post
commrepl = request.POST.get("commentID")
form.instance.comm_to_repl_id = int(commrepl)
form.save()
else:
print("some error with form happened")
print(form.errors.as_data())
return redirect(reverse("single_news", kwargs={
"slug": self.get_object().slug
}))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = Post.objects.get(slug=self.kwargs['slug'])
context['form'] = self.form
self.object.views = F('views') + 1
self.object.save()
self.object.refresh_from_db()
return context
def get_queryset(self):
return Comment.objects.filter(replies=None)
Template:
{% extends 'base.html' %}
{% load static %}
{% load sidebar %}
{% block title %}
{{ title }}
{% endblock %}
{% block header %}
{% include 'inc/_header.html'%}
{% endblock %}
{% block content %}
<section class="single-blog-area">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="border-top">
<div class="col-md-8">
<div class="blog-area">
<div class="blog-area-part">
<h2>{{ post.title}}</h2>
<h5> {{ post.created_at }}</h5>
<img src="{{ post.photo.url }}">
<div>
<span>Category: {{ post.category }}</span> <br>
<span>Posted by: {{ post.posted_by }}</span> <br>
</div>
<h5>Views: {{ post.views }}</h5>
<p>{{ post.content|safe }}</p>
<div class="commententries">
<h3>Comments</h3>
{% if user.is_authenticated %}
<form method="POST" action="{% url 'single_news' slug=post.slug %}">
{% csrf_token %}
<input type="hidden" id="commentID">
<div class="comment">
<input type="text" name="comment_content" placeholder="Comment" class="comment">
</div>
<div class="post">
<input type="submit" value="Post">
</div>
</form>
{% else %}
<h5>Login in order to leave a comment</h5>
{% endif %}
<ul class="commentlist">
{% if not post.comments.all %} </br>
<h5>No comments yet...</h5>
{% else %}
{% for comment in post.comments.all %}
<li>
<article class="comment">
<header class="comment-author">
<img src="{{ user.image.url }}" alt="">
</header>
<section class="comment-details">
<div class="author-name">
<h5>{{ comment.user_created_comment.username }}</h5>
<p>{{ comment.created }}</p>
</div>
<div class="comment-body">
<p>{{ comment.comment_content }} </p>
</div>
<div class="reply">
<p><span><i class="fa fa-thumbs-up" aria-hidden="true"></i>12</span><span><button class="fa fa-reply" aria-hidden="true"></button>7</span></p>
<form method="POST" action="{% url 'single_news' slug=post.slug %}">
{% csrf_token %}
<input type="hidden" name="commentID" value="{{ comment.id }}">
<div class="comment">
<input type="text" name="comment_content" placeholder="Comment" class="replyComment">
</div>
<div class="post">
<input type="submit" value="Reply">
</div>
</form>
</div>
</section>
{% if comment.replies.all %}
{% for reply in comment.replies.all %}
<ul class="children">
<li>
<article class="comment">
<header class="comment-author">
<img src="{% static 'img/author-2.jpg' %}" alt="">
</header>
<section class="comment-details">
<div class="author-name">
<h5>{{ reply.user_created_comment.username }}</h5>
<p>Reply to - {{ reply.comm_to_repl.user_created_comment }}</p>
<p>{{ reply.created }}</p>
</div>
<div class="comment-body">
<p>{{ reply.comment_content}}</p>
</div>
<div class="reply">
<p><span><i class="fa fa-thumbs-up" aria-hidden="true"></i>12</span><span><i class="fa fa-reply" aria-hidden="true"></i>7</span></p>
<form method="POST" action="{% url 'single_news' slug=post.slug %}">
{% csrf_token %}
<input type="hidden" name="commentID" value="{{ reply.id }}">
<div class="comment">
<input type="text" name="comment_content" placeholder="Comment" class="replyComment">
</div>
<div class="post">
<input type="submit" value="Reply">
</div>
</form>
</div>
</section>
</article>
</li>
</ul>
{% endfor %}
{% endif %}
</article>
{% endfor %}
{% endif %}
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="newsletter">
<h2 class="sidebar-title">Search for the news</h2>
<form action="{% url 'search' %}" method="get">
<input type="text" name="s" placeholder="Search...">
<input type="submit" value="Search">
</form>
</div>
{% get_popular_posts 5 %}
<div class="tags" style="">
<h2 class="sidebar-title">Tags</h2>
{% for ta in post.tag.all %}
<p>{{ ta.title }}</p>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
{% endblock %}
{% block footer %}
{% include 'inc/_footer.html' %}
{% endblock %}
Urls:
urlpatterns = [
path('', HomePage.as_view(), name='home'),
path('category/<str:slug>/', GetCategory.as_view(), name='category'),
path('post/<str:slug>', ShowSingleNews.as_view(), name='single_news'),
path('tag/<str:slug>', GetNewsByTag.as_view(), name='news_by_tag'),
path('search/', Search.as_view(), name='search'),
path('registration/', registration, name='registration'),
path('login/', loginn, name='login'),
path('logout/', logoutt, name='logout'),
forms:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['comment_content']
You need to be a bit of change in passing URL in HTML like this...
<form method="POST" action="{% url 'single_news' post.slug %}">
{% csrf_token %}
<input type="hidden" id="commentID">
<div class="comment">
<input type="text" name="comment_content" placeholder="Comment" class="comment">
</div>
<div class="post">
<input type="submit" value="Post">
</div>
</form>
NOTE:- If you want to pass url with key you can do like this
<form method="POST" action="{% url 'single_news'?slug=post.slug %}">
{% csrf_token %}
<input type="hidden" id="commentID">
<div class="comment">
<input type="text" name="comment_content" placeholder="Comment" class="comment">
</div>
<div class="post">
<input type="submit" value="Post">
</div>
</form>

Django error: 'WSGIRequest' object has no attribute 'post' for comments

I'm new to django and I'm working on a blog app and trying to enable comments for posts. When I write comments from admin page everthing works fine, but as a user I get following error: 'WSGIRequest' object has no attribute 'post'. Searching similar problems I see that mostly people switch request.Post for request.POST but I do not have that in my code, I wrote view for comment as a class not as def comment(request)… How do I solve this problem?
code from views.py:
class PostDetailView(DetailView):
model = Post
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = [title, summary, content, image]
def form_valid(self, form):
form.instance.author= self.request.user
return super().form_valid(form)
class CommentCreateView(LoginRequiredMixin, CreateView):
model = Comment
fields = ['content']
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.post = self.request.post
return super().form_valid(form)
code from models.py:
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['created']
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
def __str__(self):
return 'Comment of user {}: {}'.format(self.user, self.content)
class Post(models.Model):
title= models.CharField(max_length=100)
summary= models.TextField(max_length=200, default='defalut text ...')
content= models.TextField()
image= models.ImageField(default='default.png', upload_to='post_pics')
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.image.path)
img.save(self.image.path)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
from post_detail.html:
{% extends "recepti/base.html" %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
<small class="text-muted">{{ object.date_posted|date:"F d, Y" }}</small>
<small class="">{{ object.comments.count }}</small> <i class="fa fa-comment"></i>
<small>0</small> <i class="fa fa-heart"></i><br>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2 class="article-title">{{ object.title}}</h2>
<p class="article-title">{{ object.summary}}</p>
<img src="{{ object.image.url }}">
<p class="article-content">{{ object.content}}</p>
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-secondary">Like</button>
<a class="nav-item nav-link" href="{% url 'post-comment' %}">New comment</a>
</div>
<hr>
{% for comment in object.comments.all %}
<h5>{{ comment.user }}</h5>
<h6>{{ comment.created }}</h6>
<p>{{ comment.content }}</p>
{% empty %}
<p> No comments</p>
{% endfor %}
</div>
</article>
{% endblock content %}
code from urls.py:
urlpatterns = [
path('', PostListView.as_view(), name='recepti-home'),
path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('post/comment/', CommentCreateView.as_view(), name='post-comment'),
path('about/', views.about, name='recepti-about')
]
code from comment_form.html:
{% extends "recepti/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New comment</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Submit</button>
</div>
</form>
</div>
{% endblock content %}
I guess the "post" is related to Comment.post not to the method you are using.
So Comment consists of 4 fields (one of them is autofield) but you listed only 'comment' in your fields - add other fields or use modelform
There are couple of little issues here; First, you need a way to specify the post which is going to have the comment in some way. You can use the primary key of post for that purpose. So you need to change the url like:
urls.py:
path('post/<int:post_pk>/comment/', CommentCreateView.as_view(), name='post-comment'),
and change the action attribute of the form as well:
template:
{% extends "recepti/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST" action="{% url 'post-comment' object.pk %}"> <-------------
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New comment</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Submit</button>
</div>
</form>
</div>
{% endblock content %}
(based on the approach you used to render this template, you may need to replace post.pk with object.pk or something similar)
and finally, in the views.py, you can access the primary key of the post using self.kwargs:
class CommentCreateView(LoginRequiredMixin, CreateView):
model = Comment
fields = ['content']
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.post = self.kwargs.get('post_pk')
return super().form_valid(form)

How to add extra category field in django.views.generic

I have class in views.py:
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'content',]
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
I want to add object to fields, but when I'm trying to add like this:
fields = ['title', 'content', 'category']
the debugger send an error log like this:
FieldError at /post/new/
Unknown field(s) (category) specified for Post
I need an extra column to add category field when creating post in my django blog.
Here is blog_category.html
{% extends 'blog/base.html' %}
{% block content %}
<h1 class='mb-3'>Category: {{ category | title }}</h1>
{% for post in posts %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}" alt="">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2 author_title" href="{% url 'user-posts' post.author.username %}">#{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"N d, Y" }}</small>
<div>
<small class="text-muted">
Categories:
{% for category in post.categories.all %}
<a href="{% url 'blog_category' category.name %}">
{{ category.name }}
</a>
{% endfor %}
</small>
</div>
</div>
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
<p class="article-content">{{ post.content|slice:200 }}...</p>
</div>
</article>
{% endfor %}
{% endblock content %}
models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
categories = models.ManyToManyField('Category', related_name='posts')
image = models.ImageField(upload_to='images', default="images/None/no-img.jpg")
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Category(models.Model):
name = models.CharField(max_length=20)
My post_form.html:
{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Blog Post</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Post</button>
</div>
</form>
</div>
{% endblock content %}
The main goal is:
create category field in views.py -> after pressing "Post" this field should push the content to the django admin panel and add category to the post(as title and content adding).
If someone have a ready solution or know how to solve this problem, I would be really happy.
fields = ['title', 'content', 'categories']

Django form action

I just started with Django and already scratching my head, I have a simple form with action set to null, On submit it goes to a url which I cannot find since I have to put a condition there as to which url should it hit.
template:
{% extends 'base.html' %}
{% block body_block %}
<div class="container">
<div class="">
<h2>Education Details</h2>
<form action="" method="post">
{{ form.as_p }}
{% csrf_token %}
<div class="d-flex justify-content-end">
{% if '/attorney-profile-wizard/additional-information/' not in request.META.HTTP_REFERER %}
<a href="{% url "attorney_edit" url_key %}"
class="material-button bg-red pl-2">Cancel</a>
{% else%}
<a href="/attorney-profile-wizard/additional-information/"
class="material-button bg-red pl-2">Cancel</a>
{% endif %}
<!--<a href="{% url "attorney_edit" url_key %}"-->
<!--class="material-button bg-red pl-2">Cancel</a>-->
<input type="submit" class="material-button" name="" value="Save Education">
</div>
</form>
</div>
</div>
{% endblock %}
Model:
class Education(models.Model):
degree = models.CharField(max_length=256)
school_name = models.CharField(max_length=256)
accolades = models.TextField(null=True)
start_year = models.IntegerField(blank=True, null=True)
end_year = models.IntegerField(blank=True, null=True)
is_law_school = models.BooleanField(default=False)
position = models.PositiveSmallIntegerField(blank=True, null=True)
attorney = models.ForeignKey(AttorneyProfile, related_name='educations')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-id']
def __str__(self):
return '{} - {}'.format(self.attorney, self.degree)
views.py
class AttorneyEducationEditMixin(AttorneyEducationMixin, AttorneyEditMixin):
form_class = EducationForm
template_name = 'attorney/education/form.html'
class EducationCreateView(AttorneyEducationEditMixin, CreateView):
permission_required = 'educations.add_education'
class EducationUpdateView(AttorneyEducationEditMixin, EducationPermissionMixin,
UpdateView):
permission_required = 'educations.change_education'
class EducationDeleteView(AttorneyEducationMixin, EducationPermissionMixin,
DeleteView):
template_name = "attorney/shared/title_delete.html"
permission_required = 'educations.delete_education'
I even tried to override the form action trying the following way:
{% if '/attorney-profile-wizard/additional-information/' not in request.META.HTTP_REFERER %}
<form action="{% url "attorney_edit" url_key %}" method="post">
{% else %}
<form action="/attorney-profile-wizard/additional-information/" method="post">
{% endif %}
{{ form.as_p }}
{% csrf_token %}
<div class="d-flex justify-content-end">
{% if '/attorney-profile-wizard/additional-information/' not in request.META.HTTP_REFERER %}
<a href="{% url "attorney_edit" url_key %}"
class="material-button bg-red pl-2">Cancel</a>
{% else%}
<a href="/attorney-profile-wizard/additional-information/"
class="material-button bg-red pl-2">Cancel</a>
{% endif %}
<!--<a href="{% url "attorney_edit" url_key %}"-->
<!--class="material-button bg-red pl-2">Cancel</a>-->
<input type="submit" class="material-button" name="" value="Save Education">
</div>
</form>
This did not work and obviously is not the right way to do so. Where would I find the url for the action? I even checked urls.py file but it did not help much.

How to customize html of one form field in a form?

I need the user to enter data into a form in my django app. I am currently doing this like so:
forms.py: (using model form)
class BabysittingForm(forms.ModelForm):
class Meta:
model = models.Babysitting
fields = [
'title',
'pay',
'start_date',
'end_date',
'num_children',
'babysitting_address',
'babysitting_day_of_week',
'babysitting_start_time_of_day',
'babysitting_session_length_hours',
'description',
]
Here is my models.py:
class Job(models.Model):
employer = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="jobs")
hired_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
created_at = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(default=datetime.now())
end_date = models.DateTimeField(default=datetime.now())
location = models.CharField(default="None", max_length=40)
title = models.CharField(max_length=30)
description = models.TextField()
category = models.CharField(max_length=3, choices=JOB_CATEGORY_CHOICES)
pay = models.FloatField()
hourly_pay = models.BooleanField(default=True)
complete = models.BooleanField(default=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('jobs:detail', kwargs={
'job_pk': self.id
}
)
class Babysitting(Job):
num_children = models.PositiveSmallIntegerField()
babysitting_address = models.CharField(default="None", max_length=40)
babysitting_day_of_week = models.CharField(max_length=2, choices=DAYS_OF_WEEK_CHOICES)
babysitting_start_time_of_day = models.CharField(max_length=8)
babysitting_session_length_hours = models.FloatField()
Here is my layout:
{% extends 'layout.html' %}
{% load bootstrap3 %}
{% block title %}{{ form.instance.title|default:"New Job" }}{% endblock %}
{% block content %}
<div class="container">
<h1>{{ form.instance.title|default:"New Job" }}</h1>
<form action="" method="POST">
{% bootstrap_form form %}
{% csrf_token %}
<input type="submit" value="Post Job" class="btn btn-default"/>
</form>
</div>
{% endblock %}
I need to change the appearance of the pay form field to this:
I know how to style it this way but I don't know how to change the form template to do this.
How do you customize a single form's appearance in django?
Sorry I should have been more specific, I want to replace one of the form fields with this html (bootstrap):
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</div><!-- /btn-group -->
<input type="text" class="form-control" aria-label="...">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div>
Thank you.
Maybe you could use django-widget-tweaks
If you need to customize your field class, you can render one by one and to add a class to each field
ie, in your template
{% extends 'layout.html' %}
{% load bootstrap3 %}
{% load widget_tweaks %}
{% block title %}{{ form.instance.title|default:"New Job" }}{% endblock %}
{% block content %}
<div class="container">
<h1>{{ form.instance.title|default:"New Job" }}</h1>
<form action="" method="POST">
{% csrf_token %}
<!-- ie field 1 -->
{% render_field form.field1 class+="form-control other_class" %}
<input type="submit" value="Post Job" class="btn btn-default"/>
</form>
</div>
{% endblock %}
Documentation about django-widget-tweaks here
If you want to style your form with custom CSS classes, ids etc you should override the widget class attribute of the Meta class of your form. More on this in overriding the default fields.
Example:
# forms.py
from django.forms import ModelForm
from .models import Babysitting
class BabysittingForm(forms.ModelForm):
class Meta:
model = Babysitting
fields = [...] # as is
widgets = {
# add here any css classes or attributes per field (on the specific widget)
'title': forms.TextInput(attrs={'class': 'custom-class', 'id': 'custom-id',}),
'pay': forms.NumberInput(attrs={'class': 'another-class', 'id': 'another-id',}),
}

Categories