Issue
I have a problem with the image uploading from form (when i upload image from admin panel it works). Image just dont uploading, without any feedback and log. Help me pls
My view.py:
def class_article_create(request, URLcodenumber):
print(URLcodenumber)
if request.method == 'POST':
model = Article()
form = ArticleForm(
request.POST,
instance=model
)
print(request.method)
new_article = form.save(commit=False)
new_article.codenumber = Classes.objects.filter(
codenumber=URLcodenumber
)[0]
new_article.pub_date = date.today()
print(new_article.id)
if form.is_valid():
new_article.save()
return HttpResponseRedirect(f'/class/{URLcodenumber}')
return render(
request,
'blogpage\\create_article.html',
{
'clsName': f'Параллель {URLcodenumber}',
'codenumber': URLcodenumber,
'form': ArticleForm()
}
)
My models.py:
class Article(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
headline = models.CharField(
max_length=50,
default='Нет заголовка'
)
text = models.TextField(
max_length=600,
)
image = models.ImageField(
upload_to='images/',
blank=True
)
pub_date = models.DateField()
codenumber = models.ForeignKey(
'Classes',
on_delete=models.CASCADE
)
create_article.html
{% extends "blogpage\layout\base.html" %}
{% load static %}
{% block title %}
Добавление ответа
{% endblock title %}
{% block nav %}
<div class="classname-div">
<p class="cls-name" align="center">{{clsName}}</p>
</div>
<a href='{% url "classpage_articles_preview" URLcodenumber=codenumber %}'>
<button class="btn-to-page">Перейти к ответам</button>
</a>
{% endblock nav %}
{% block content %}
<form method="post" class="model-form" enctype="multipart/form-data">
{% csrf_token %}
{{form.headline}}
{{form.text}}
{{form.image}}
<button class="model-form-submit" type="submit">
Добавить
</button>
</form>
{% endblock content %}
early, there wasnt the enctype="multipart/form-data", and the problem was same
media root and url in settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import re_path
from django.conf import settings
from django.conf.urls.static import static
from help_page.helppage_views import help_page
from start_page import startpage_views
from help_page import helppage_views
from class_page import classpage_views
urlpatterns = [
path('admin/', admin.site.urls),
path(
'',
startpage_views.start_page,
name='startpage'
),
path(
'help/<numpage>/',
helppage_views.help_page,
name='helppage'
),
path(
'class/<URLcodenumber>/',
classpage_views.class_article_preview,
name='classpage_articles_preview'
),
path(
'create/<URLcodenumber>',
classpage_views.class_article_create,
name='classpage_articles_create'
),
path(
'view/<URLcodenumber>/<uuid:uuid>',
classpage_views.class_article_view,
name='classpage_article_view'
),
path(
'createclass',
classpage_views.create_class,
name='create_class'
)
] + static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
proj_dir => media => images is directory for the images
Thanks for any help! :)
Solution
You need to pass both request.POST
and request.FILES
to the form:
from django.shortcuts import get_object_or_404
def class_article_create(request, URLcodenumber):
if request.method == 'POST':
code_number = get_object_or_404(Classes, codenumber=URLcodenumber)
form = ArticleForm(request.POST, request.FILES)
if form.is_valid():
form.instance.codenumber = code_number
form.instance.pub_date = date.today()
form.save()
return HttpResponseRedirect(f'/class/{URLcodenumber}')
else:
form = ArticleForm()
return render(
request,
'blogpage/create_article.html',
{
'clsName': f'Параллель {URLcodenumber}',
'codenumber': URLcodenumber,
'form': form,
},
)
Note: Django's
DateTimeField
[Django-doc] has aauto_now_add=…
parameter [Django-doc] to work with timestamps. This will automatically assign the current datetime when creating the object, and mark it as non-editable (editable=False
), such that it does not appear inModelForm
s by default.
Answered By - Willem Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.