How can I display images in Django after uploading? - python

I'm making a small blog. And I will be creating a cover image uploading system.
But after uploading, I'm unable to retrieve the image.
Do you have any solutions?
Here's my model.py:
class Post(models.Model):
id = models.AutoField
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content_images = models.ImageField(default="media/noimg-l.jpg")
and here's form.py:
class Createcontent(forms.ModelForm):
class Meta:
model = Post
fields = (
"title",
"content",
"slug",
"content_images",
"status",
)
file = forms.FileField()
widgets = {
"content": SummernoteWidget(),
}
and this is the HTML I'm using:
<div class="row">
<form method="POST" enctype="multipart/form-data" class="post-form">{% csrf_token %} {{ form|crispy }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
</div>
and my setting.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
X_FRAME_OPTIONS = 'SAMEORIGIN'
SUMMERNOTE_THEME = 'bs4' # Show summernote with Bootstrap4
and my views.py
ef create_post(request):
if request.method == "POST":
form = Createcontent(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
return redirect('post_detail', post.slug)
else:
form = Createcontent()
return render(request, 'blog/post_creator.html', {'form': form})

Related

uploading images through form.py

if i upload the image through my admin dashboard, the image will be successfully uploaded, it will appear in media folder in my project directory i specified in settings.py. but if i upload an image through form.py as a user, every other field is saved except for the image field. I've tried most of the solutions on stackoverflow, dont know why mine ain't working.
while debugging i made mainimage a required field, so it threw this error: ValueError: The view products.views.products didn't return an HttpResponse object. It returned None instead.
form.py
from django import forms
from django.utils.translation import gettext_lazy as _
from .models import Product
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ['name', 'mainimage', 'category', 'preview_text',
'detail_text', 'price','Quantity']
labels = {
'name': _('Product name'),
}
help_texts = {
'name': _('be specific eg Tomatoes, Rice'),
}
error_messages = {
'name': {
'max_length': _("This writer's name is too long."),
},
}
views.py
from django.shortcuts import render, redirect
from .form import ProductForm
from .models import Product
# Create your views here.
def products(request):
if request.method == 'GET':
form = ProductForm()
return render(request, 'products/add_product.html',{'forms':form})
else:
# imagefield is different from other
# fields and it needs to be handles properly
# data fetched from imagefield would be stored
# in request.FILES object.
if request.method == 'POST':
form = ProductForm(request.POST or None, request.FILES or None)
if form.is_valid():
name = form.cleaned_data.get('name')
mainimage = form.cleaned_data.get('mainimage')
category = form.cleaned_data.get('category')
preview_text = form.cleaned_data.get('preview_text')
detail_text = form.cleaned_data.get('detail_text')
price = form.cleaned_data.get('price')
Quantity = form.cleaned_data.get('Quantity')
obj = Product.objects.create(
name = name,
mainimage = mainimage,
category = category,
preview_text = preview_text,
detail_text = detail_text,
price = price,
Quantity = Quantity
)
obj.save()
# form.save()
return redirect('/products/')
else:
form = ProductForm()
return render(request, 'add_product.html', {'form': form})
add_product
{% extends "plugs/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<h4>Add a Product</h4>
<form action="#" method="post" autocomplete="off" enctype="multipart/form-data">
{% csrf_token %}
<!-- {{form_store.store_name|as_crispy_field}}-->
<div class="col-md-3">
{{ forms.name|as_crispy_field }}
</div>
<div class="row">
<div class="col-md-4" >
{{ forms.mainimage|as_crispy_field }}
</div>
<div class="col-md-3">
{{ forms.category|as_crispy_field }}
</div>
</div>
<div class="col-md-5">
{{ forms.preview_text|as_crispy_field }}
{{ forms.detail_text|as_crispy_field }}
<!-- {{ forms.price|as_crispy_field }}-->
{{ forms.Quantity|as_crispy_field }}
</div>
<button type="submit" class="btn btn-success"><i class="fas fa-database"></i>Submit</button>
</form>
{% endblock %}
settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/static/media/')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_URL = '/media/'
models.py
from django.db import models
# Create your models here.
class Category(models.Model):
title = models.CharField(max_length= 300)
primaryCategory = models.BooleanField(default=False)
def __str__(self):
return self.title
class Product(models.Model):
mainimage = models.ImageField(upload_to='products/', blank=True)
name = models.CharField(max_length=300)
slug = models.SlugField(blank = True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
preview_text = models.TextField(max_length = 200, verbose_name='Preview Text')
detail_text = models.TextField(max_length= 1000, verbose_name='Detail text')
price = models.FloatField()
Quantity = models.CharField(max_length=150, default='1 quantity')
def __str__(self):
return self.name
project/urls.py
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('plugs.urls')),
path('products/', include('products.urls')),
path('home/', include('cart.urls')),
# path('products/', include('products.urls')),
]
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns +=static(settings.MEDIA_URL,
document_root = settings.MEDIA_ROOT)
ok better change your view to
if request.method == 'POST':
form = ProductForm(request.POST or None, request.FILES or None)
if form.is_valid():
instance = form.save(commit=False)
instance.name = form.cleaned_data['name']
instance.mainimage = form.cleaned_data['mainimage']
instance.category = form.cleaned_data['category']
instance.preview_text = form.cleaned_data['preview_text']
instance.detail_text = form.cleaned_data.get['detail_text']
instance.price = form.cleaned_data['price']
instance.Quantity = form.cleaned_data['Quantity']
instance.save()
Is your MEDIA_DIR set in your settings.py?
MEDIA_DIR = os.path.join(BASE_DIR, '/static/media/')
ValueError: The view products.views.products didn't return an HttpResponse object. It returned None instead. Means your somewhere in your views you are not returning an Http response. Try indenting last line of your view to at same level as else. It may resolve

How to add comments on django

I am trying to add a comment system to my project, all the code looks fine but I am getting this error "ValueError at /
The QuerySet value for an exact lookup must be limited to one result using slicing". I dont know what is wrong but the error might be on the views.py file.
views.py
def imagelist(request):
images = Post.objects.all()
post = get_object_or_404(Post)
comments = Comment.objects.filter(post=images)
if request.method == 'POST':
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
contentt = request.POST.get('content')
comment = Comment.objects.create(post=images, user=request.user, content=content)
comment.save()
return HttpResponseRedirect(post.get_absolute_url())
else:
comment_form = CommentForm()
context2 = {
"images": images,
"comments": comments,
"comment_form": comment_form,
}
return render(request, 'imagelist.html', context2)
models.py
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField(max_length=160)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '{}-{}'.format(self.post.title.str(self.user.username))
class Post(models.Model):
text = models.CharField(max_length=200)
posti = models.ImageField(upload_to='media/images', null=True, blank="True")
video = models.FileField(upload_to='media/images', null=True, blank="True")
user = models.ForeignKey(User, related_name='imageuser', on_delete=models.CASCADE, default='username')
liked = models.ManyToManyField(User, default=None, blank=True, related_name='liked')
updated = models.DateTimeField(auto_now=True)
created =models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.tittle)
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('content',)
You need to pass to the create mathod of the comments a single Post since the corresponding fields is a ForeignKey and you're passing an entire queryset (Post.objects.all())
You need get just the post where the comment should live.
single_post = Post.objects.get(pk=the_post_pk)
comment = Comment.objects.create(post=single_post, user=request.user, content=content)
The problem is that you write:
images = Post.objects.all()
comments = Comment.objects.filter(post=images)
Here images is a set of Post objects, not a single Post object, hence you can not filter on that. But you actually do not need to do this anyway.
Furthermore there is also a small mistake in the __str__ of your Comment model:
class Comment(models.Model):
# …
def __str__(self):
return '{}-{}'.format(self.post.text, self.user.username)
But the view itself looks "odd", since you here write a list of Posts, but if you make a POST request, you will somehow need to know to what post you want to submit the Comment, therefore at least the view that accepts the POST request, will need to know the primary key of the Post to comment on. You can for example encode that in the urls.py:
# appname/urls.py
from django.urls import path
from app import views
urlpatterns = [
path('post/<int:pk>/', views.post_detail, name='post_detail'),
path('post/', views.post_list, name='post_detail')
]
In that view, we can then fetch the item, for example with get_object_or_404, and in case of a POST set the post and user objects in the view:
from django.shortcuts import redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
#login_required
def post_detail(request, pk):
image = get_object_or_404(Post, pk=pk)
if request.method == 'POST':
comment_form = CommentForm(request.POST, request.FILES)
if comment_form.is_valid():
comment_form.instance.user = request.user
comment_form.instance.post = image
comment_form.save()
return redirect(post)
else:
comment_form = CommentForm()
context = {
'post': image,
'comment_form': comment_form,
}
return render(request, 'imagedetail.html', context)
In your template, you can render the comments of the post with:
{{ post }}
{% for comment in post.comment_set.all %}
{{ comment }}
{% endfor %}
<form method="post" action="{% url 'post_detail' pk=post.pk %}">
{% csrf_token %}
{{ comment_form }}
</form>
you can also make a view that renders a list:
#login_required
def post_list(request, pk):
images = Post.objects.prefetch_related('comment_set')
comment_form = CommentForm()
context = {
'image': images,
'comment_form': comment_form,
}
return render(request, 'imagelist.html', context)
in the template for the list, you can render it with:
{% for post in images %}
{{ post }}
{% for comment in post.comment_set.all %}
{{ comment }}
{% endfor %}
<form method="post" action="{% url 'post-detail' pk=post.pk %}">
{% csrf_token %}
{{ comment_form }}
</form>
{% endfor %}
here we thus make a POST request to the post-detail view.
After 4 hours of searching for answers, this is how I achieve it. All I did was add this new view, method, url and html. Hope this helps!
views.py
def imagedetail(request, pk):
post = get_object_or_404(Post, pk=pk)
comments = Comment.objects.filter(post=post)
if request.method == 'POST':
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
content = request.POST.get('content')
comment = Comment.objects.create(post=post, user=request.user, content=content)
comment.save()
return HttpResponseRedirect(post.get_absolute_url())
else:
comment_form = CommentForm()
context2 = {
"comments": comments,
"comment_form": comment_form,
}
return render(request, 'imagedetail.html', context2)
models.py (on Post model)
def get_absolute_url(self):
return reverse('imagedetail', args=[self.id])
urls.py (for new view which is imagedetail)
path('imagedetail/<int:pk>/', views.imagedetail, name='imagedetail'),
imagelist.html (to redirect to imagedetail, btw it is a bootstrap button)
<a role="button" class="btn btn-primary" href="{% url 'imagedetail' pk=image.pk %}"></a>
imagedetail.html (it only shows comments)
<form method="post">
{% csrf_token %}
{{comment_form.as_p}}
{% if request.user.is_authenticated %}
<input type="submit" value="Submit" class="btn btn-outline-succes">
{% else %}
<input type="submit" value="Submit" class="btn btn-outline-succes" disabled>
{% endif %}
</form>
<div class="main-comment-section">
{{ comments.count }}
{% for comment in comments %}
<blockquote class="blockquote">
<p class="mb-0">{{ comment.content }}</p>
<footer class="blockquote-footer">by <cite title="Source Title">{{ comment.user }}</cite></footer>
</blockquote>
{% endfor %}
</div>
2 - forms.py icine elave ele:
from django import forms
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'body')
# overriding default form setting and adding bootstrap class
def __init__(self, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs = {'placeholder': 'Enter name', 'class': 'form-control'}
self.fields['body'].widget.attrs = {'placeholder': 'Comment here...', 'class': 'form-control', 'rows': '5'}
3 - models.py elave ele :
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")
name = models.CharField(max_length=50)
parent = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ('created',)
def __str__(self):
return self.name
def get_comments(self):
return Comment.objects.filter(parent=self).filter(active=True)
4 - views.py post bele gorunmelidi :
def post (request,slug):
post = Post.objects.get(slug = slug)
latest = Post.objects.order_by('-timestamp')[:3]
comments = post.comments.filter(active=True)
new_comment = None
comment_form = CommentForm()
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.post = post
new_comment.save()
comment_form = CommentForm()
context = {
'post': post,
'latest': latest,
'comments': comments,
'comment_form': comment_form
}
return render(request, 'post.html', context)
5 - post.html de author altina divider den sonra elave ele :
<hr/>
<h3>Add Comment</h3>
<form method="post" action="">
{% csrf_token %}
{{ comment_form.as_p }}
<button type="submit" class="btn btn-primary">Comment</button>
</form>
{% with comments.count as total_comments %}
<h3 class="mt-5">
{{ total_comments }} comment{{ total_comments|pluralize }}
</h3>
{% endwith %}
{% if not post.comments.all %}
No comments yet
{% else %}
{% for comment in post.get_comments %}
{% include 'comment.html' with comment=comment %}
{% endfor %}
{% endif %}
</div>
6 - admin.py elave ele :
#admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display=('name', 'post', 'created', 'active')
list_filter = ('active', 'created', 'updated')
search_fields = ('name', 'body')
7 - templates de comment.html yarat ve icine elave et :
<div class="border-0 border-start border-2 ps-2" id="{{comment.id}}">
<div class="mt-3">
<strong>{{comment.name}}</strong>
{% if comment.parent.name%} to <strong>{{comment.parent.name}}</strong>{% endif %}
<small class="text-muted">On {{ comment.created.date }}</small>
</div>
<div class="border p-2 rounded">
<p>{{comment.body}}</p>
<button class="btn btn-primary btn-sm" onclick="handleReply({{comment.id}})">Reply</button>
<div id="reply-form-container-{{comment.id}}" style="display:none">
</div>
</div>
{% for comment in comment.get_comments %}
{% include 'comment.html' with comment=comment %}
{% endfor %}
8 - models de post un altina elave ele
def get_comments(self):
return self.comments.filter(parent=None).filter(active=True)

How to place default value in to dropdown form field in django

I'm building a post-comment model in one view, one page, something like facebook. I have two forms in my home.html and view.py: new post and new comment. In each post container, there is a new comment form.
I have a problem because I don't know how to relate comment to post - specifically how to pass post.id to my comment form.
Is it possible to pass my {{ post.id }} to my {{newCommentForm.field }}? That each comment has a default value of post's id?
My home.html:
{% for post in posts %}
<div class="container">
<a class="user" href="#">{{ post.author }}</a>, {{ post.date_posted }}
<img src="{{ post.author.profile.image.url }}" alt="{{ post.author }}"style="width:100%;">
<p>{{ post.content }}</p>
<form METHOD="POST" class="new_post">
{% csrf_token %}
{{ newCommentForm.content }}
{{ newCommentForm.post }}
<button type="submit" name="newCommentSubmit">Add</button>
</form>
</div>
{% endfor %}
models.py
class Post(models.Model):
content = models.TextField(max_length=1000)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.content
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField(max_length=500)
date = models.DateTimeField(default=timezone.now)
def add_coment(self):
self.date = timezone.now()
self.save()
def __str__(self):
return self.content
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username}'
views.py
#login_required()
def home(request):
newPostForm = newPost()
newCommentForm = newComment()
if request.method == 'POST':
if 'newPostSubmit' in request.POST:
newPostForm = newPost(request.POST, prefix='newpost')
if newPostForm.is_valid():
instance = newPostForm.save(commit=False)
instance.author = request.user
instance.date_posted = timezone.now()
instance.save()
newCommentForm = newComment(prefix='newcomment')
elif 'newCommentSubmit' in request.POST:
newCommentForm = newComment(request.POST, prefix='newcomment')
if newCommentForm.is_valid():
instance = newCommentForm.save(commit=False)
instance.author = request.user
instance.date_posted = timezone.now()
instance.save()
newPostForm = newPost(prefix='newpost')
else:
newPostForm = newPost(prefix='newpost')
newCommentForm = newComment(prefix='newcomment')
context = {
'newPostForm': newPostForm,
'newCommentForm': newCommentForm,
'posts': Post.objects.all().order_by('-date_posted'),
'comments': Comment.objects.all()
}
return render(request, 'blog/home.html', context)
def about(request):
return render(request, 'blog/about.html')
My model is working now, comments are added, but I need to choose my post (post.id) manually from the default dropdown field witch all posts.
Add your post to comment like this:
if newCommentForm.is_valid():
instance = newCommentForm.save(commit=False)
instance.author = request.user
instance,=.post = request.post
instance.date_posted = timezone.now()
instance.save()
And send post in request!

Image not saving in UserProfileForm (Django)

I have a model form to update the user profile and everything is saving correctly except of them image. If I use admin it updates fine but when I use my form it just stays as the default profile image.
Here is my form:
class EditProfileForm(forms.ModelForm):
birth_date = forms.DateField(label='birth_date', input_formats=['%Y-%m-%d'])
class Meta:
model = UserProfile
fields = (
"image",
"bio",
"location",
"birth_date",
)
Here is my model:
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
image = models.ImageField(upload_to='profile_image', blank=True)
def __str__(self):
return self.user.username
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=User)
Here is my view:
def edit_profile(request):
instance = get_object_or_404(UserProfile, user=request.user)
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return redirect('/')
else:
form = EditProfileForm(instance=request.user)
return render(request, 'edit_profile.html', {'form': form})
And here is my html:
{% extends 'base.html' %}
{% block content %}
<h1>Edit Profile</h1>
<form method='POST' action=''>{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-outline-success">Save</button>
</form>
</body>
{% endblock %}
For file upload you need to specify form's enctype:
<form method='POST' action='' enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-outline-success">Save</button>
</form>
And you should pass request's files to form instance in view:
form = EditProfileForm(request.POST, request.FILES, instance=instance)
Check this doc for details.

Django form doesn't show up

I'm working on some project and ended up with some issues.
So my form doesn't display itself at all in my template.
But I've created another form before and it works as it should!
So my code:
models.py
class Project(models.Model):
class Meta:
db_table = "project"
COLORS = (
('R', 'Red'),
('B', 'Blue'),
('G', 'Green'),
('Y', 'Yellow')
)
project_title = models.CharField(max_length=200)
project_color = models.CharField(max_length=1, choices=COLORS)
def __str__(self):
return self.project_title
forms.py
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = ['project_title', 'project_color']
views.py
def addproject(request):
if request.POST:
form_p = ProjectForm(request.POST)
if form_p.is_valid():
form_p.save(commit=False)
return HttpResponseRedirect('/')
else:
form_p = ProjectForm()
context = {
'projects': Project.objects.all(),
"form": form_p,
'username': auth.get_user(request).username,
}
context.update(csrf(request))
return render(request, 'index.html', context)
urls.py
urlpatterns = [
url(r'^addproject/$', views.addproject, name='addproject'),]
index.html
<form action="/addproject/" method="post">
{% csrf_token %}
{{ form_p.as_table }}
<button type="submit" class="btn btn-primary">Add Project</button>
</form>
The problem is within your template, you are calling your context variable as form_p while passing it as "form":
index.html
<form action="/addproject/" method="post">
{% csrf_token %}
{{ form.as_table }}
<button type="submit" class="btn btn-primary">Add Project</button>
</form>
Make sure you are using "ModelForm" instead "Form"
class NewAccountForm(forms.ModelForm):
class Meta:
model = NewAccount
fields = ('name',)
Have you imported the form in to the views.py?
place this in to your views.py
from .forms import ProjectForm

Categories

Resources