Re: guys help me please, I cannot upload images from django form,

7 views
Skip to first unread message

Jonathan Villavicencio

unread,
Aug 29, 2020, 12:54:17 PM8/29/20
to django...@googlegroups.com
Hello! 
in the class Posts in the field "image" add upload_to=upload_path for example:
image = models.ImageField(blank=True, null=True, verbose_name='surat', upload_to=upload_path)  

upload_path is a method 
write it before of the class definition
def upload_path(instance, filename):
    return '/'.join([instance.image, filename])

El sáb., 29 de ago. de 2020 a la(s) 11:16, allaberdi...@gmail.com (allaberdi...@gmail.com) escribió:
I can upload images from django admin panel but cannot to do same thing with django form ,  it saves any text but did not save images only,  anyone help me  please 
this is my code:

models.py:
class Posts(models.Model):

Ahal = 'Ahal'
Balkan = 'Balkan'
Dasoguz = 'Dasoguz'
Lebap = 'Lebap'
Mary = 'Mary'


CHOOSE_REGION = {
(Ahal,'Ahal'),
(Balkan,'Balkan'),
(Dasoguz,'Dasoguz'),
(Lebap,'Lebap'),
(Mary,'Mary')


name = models.CharField(max_length=30, blank=False, verbose_name='Ady')
image = models.ImageField(blank=True, null=True, verbose_name='surat')
text = models.TextField(verbose_name='gosmaça maglumat')
price = models.FloatField(verbose_name='bahasy')
region = models.CharField(max_length=30, choices=CHOOSE_REGION, blank=False, verbose_name='welaýat')
district = models.CharField(max_length=30, blank=False, verbose_name='etrab')
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='eýesi')
phone = models.CharField(max_length=50, blank=False, verbose_name='telefon belgisi')
published = models.DateTimeField(auto_now=True, verbose_name='satuwa çykan guni')



forms.py:
class PostForm(forms.ModelForm):
class Meta:
model = Posts
fields = ['name','image','text','price','region','district','user','phone']


views.py:
def add_post(request):
form = PostForm(request.POST or None)
if form.is_valid():
form.save()
template = 'add_post.html'
context = {'form':form}
return render(request,template,context)

settings.py:

STATIC_URL = '/static/'
STATICFILES_DIR = [Path(BASE_DIR,'static')]

MEDIA_URL = '/media/'
MEDIA_ROOT = Path(BASE_DIR,'media')


urls.py:

from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('posts.urls')),

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



html form tag:
{% extends 'index.html' %}

{% load static %}

{% block content %}

<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.media}}
{{form.as_p}}
<input type="submit" value="Save" name="">
</form>

{% endblock %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ad47578f-64a8-40b5-b72b-f3eebb668dben%40googlegroups.com.

Chelsea Fan

unread,
Aug 29, 2020, 2:37:32 PM8/29/20
to django...@googlegroups.com
ok bro I'll try it, thanks for now 

Reply all
Reply to author
Forward
0 new messages