18
Авг
2017

Вывод картинки в пост django

Пытаюсь вывести картинку в свой блог, но вместо этого получаю ошибку:

OperationalError at /admin/blog/post/.

В setting.py прописал:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

в models.py:

from django.db import models
from django.utils import timezone

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)
    image = models.ImageField(upload_to='static/media', blank=True)

def publish(self):
    self.published_date = timezone.now()
    self.save()

def __str__(self):
    return self.title

в urls.py:

from django.conf.urls import url
from . import views
from django.conf.urls.static import static
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings

urlpatterns = [
   url(r'^$', views.post_list, name='post_list'),
   url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
   url(r'^post/new/$', views.post_new, name='post_new'),
   url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
   url(r'^media/(?P<path>.*)$','django.views.static.serve',
   {'document_root':'media'}),
   ]  + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

и вывожу вот так:

{% extends 'blog/base.html' %}
 <body class="w3-light-grey">
  {% block content %}
    {% for post in posts %}
      <h3><b><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}
      </a></b></h3>
      <span class="w3-opacity">{{ post.published_date }}</span>    
      <p>{{ post.text|linebreaksbr }}</p>
      <img src="{{ post.image.url }}">  
    {% endfor %}  
  {% endblock content %}   

файл views.py

from django.shortcuts import render
from django.utils import timezone
from django.shortcuts import render, get_object_or_404
from .models import Post
from .forms import PostForm
from django.shortcuts import redirect

def post_list(request):  
posts =   

Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})

def post_new(request):
    if request.method == "POST":
    form = PostForm(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', pk=post.pk)
else:
    form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})   

def post_edit(request, pk):
   post = get_object_or_404(Post, pk=pk)
   if request.method == "POST":
    form = PostForm(request.POST, instance=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', pk=post.pk)
else:
    form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})    

Источник: https://ru.stackoverflow.com/questions/707816/%D0%92%D1%8B%D0%B2%D0%BE%D0%B4-%D0%BA%D0%B0%D1%80%D1%82%D0%B8%D0%BD%D0%BA%D0%B8-%D0%B2-%D0%BF%D0%BE%D1%81%D1%82-django

Share

Тебе может это понравится...