0
0 комментариев

Python 3.5 / Linux Mint /

Ошибка в логе:

[13/Nov/2019 20:11:36] \»GET /tovar/2/add_picture/ HTTP/1.1\» 200 1092
[13/Nov/2019 20:11:36] \»GET /static/mystyle.css HTTP/1.1\» 200 102
Not Found: /favicon.ico
[13/Nov/2019 20:11:36] \»GET /favicon.ico HTTP/1.1\» 404 2586
[13/Nov/2019 20:11:50] \»POST /tovar/2/add_picture/ HTTP/1.1\» 302 0
[13/Nov/2019 20:11:50] \»GET /tovar/ HTTP/1.1\» 200 3907
[13/Nov/2019 20:11:50] \»GET /static/mystyle.css HTTP/1.1\» 200 102
Not Found: /tovar/3/picture/
[13/Nov/2019 20:11:50] \»GET /tovar/3/picture/ HTTP/1.1\» 404 1720
Not Found: /tovar/4/picture/
[13/Nov/2019 20:11:50] \»GET /tovar/4/picture/ HTTP/1.1\» 404 1720
[13/Nov/2019 20:11:50] \»GET /tovar/6/picture/ HTTP/1.1\» 200 26
[13/Nov/2019 20:11:50] \»GET /tovar/2/picture/ HTTP/1.1\» 200 26
[13/Nov/2019 20:11:50] \»GET /tovar/1/picture/ HTTP/1.1\» 200 26

tovar_picture.html

[apcode language=\»xml\»]

{% extends \'generic.html\' %}
 {% block contents %}
 <form method=\"POST\" enctype=\"multipart/form-data\">{% csrf_token %}
  {{ form.as_p }}
  <p><input type=\"submit\" /></p>
</form>
 {% endblock %}

[/apcode]

index.html

[apcode language=\»xml\»]

{% extends \'generic.html\'%}
 {%block title %}Список товаров{% endblock %}
{% block header%}Список товаров{% endblock %}
 {% block contents %}
<table border=\"1\">
 <tr>
  <th>Артикул</th>
  <th>Наименование</th>
  <th>Наличие</th>
  <th>&nbsp;</th>
  <th>&nbsp;</th>
 </tr>
 {% for t in all_tovars %}
 <tr>
  <td>{{ t.article  }}</td>   <!-- Артикул      -->
  <td>{{ t.title    }}</td>   <!-- Наименование -->
  <td>{{ t.quantity }}</td>   <!-- Наличие      -->
  <td>
      {% for tag in t.tags.all|slice:\':5\' %}
        {{ tag.title }}
      {% endfor %}
  </td>
  <td><a href=\"{% url \'tovar:tags\'   t.pk %}\">Теги</a></td>
  <td><a href=\"{% url \'tovar:show\'   t.pk %}\">Просмотр</a></td>
  <td><a href=\"{% url \'tovar:edit\'   t.pk %}\">Редактирование</a></td>
  <td><a href=\"{% url \'tovar:delete\' t.pk %}\">Удалить</a></td>
  <td><a href=\"{% url \'tovar:add_picture\' t.pk %}\">Добавить картинку</a></td>
  <td><img src=\"{% url \'tovar:picture\' t.pk %}\" alt=\"картинка\" /></td>
 </tr>
 {% endfor %}
</table>
 <p><a href=\"{% url \'tovar:create\' %}\">Новый товар</a></p>
 {% endblock %}

[/apcode]

add_picture.py

[apcode language=\»python\»]

from django.shortcuts import render
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.urls import reverse
from django.db import connection
from restful import restful
from ..forms import PictureForm
  def picture(request, id_tovar):
    with connection.cursor() as cursor:
        cursor.execute(
            \'select picture from tovar_tovar where id = %s ;\',
            [ id_tovar ]
        )
        ( data, ) = cursor.fetchone()
    if not data:
        raise Http404()
    response = HttpResponse(content_type=\'image/jpeg\')
    response.write(data)
    return response
  @restful
def add_picture(request, id_tovar):
    form = PictureForm()
    return render(request, \'tovar/tovar_picture.html\', locals())
 @add_picture.method(\'POST\')
def add_picture(request, id_tovar):
    form = PictureForm(request.POST, request.FILES)
    if not form.is_valid():
        return render(request, \'tovar/tovar_picture.html\', locals())
    data = b\'\'
    for chunk in request.FILES[\'picture\'].chunks():
        data += chunk
    with connection.cursor() as cursor:
        cursor.execute(
            \'update tovar_tovar set picture=%s where id=%s ;\',
            [data, id_tovar]
        )
    return HttpResponseRedirect(reverse(\'tovar:index\'))

[/apcode]

forms.py

[apcode language=\»python\»]

from django import forms
from .models import Tovar
  class PictureForm(forms.Form):
    picture = forms.FileField(label=\'картинка\')
  class TagListForm(forms.Form):
    tag_list = forms.CharField(label=\'новые\', required=False)
  class TovarForm(forms.Form):
         title = forms.CharField(label=\'Наименование\', max_length=256)
    article = forms.CharField(label=\'Артикул\', max_length=16)
    quantity = forms.IntegerField(label=\'В наличии\', min_value=0)
    description = forms.CharField(label=\'Описание\', widget=forms.Textarea, required=False)
     class TovarModelForm(forms.ModelForm):
    class Meta:
        model = Tovar
        fields = [\'title\', \'article\', \'quantity\', \'description\']

[/apcode]

models.py

[apcode language=\»python\»]

from django.db import models
 # Create your models here.
 #TODO: Добавить значение default 0 в базу данных для поля quantity
 class Tovar(models.Model):
    title = models.CharField(max_length=255, unique=True)
    article = models.CharField(max_length=16, null=True, blank=True, unique=True)
    description = models.TextField(null=True, blank=True)
    quantity = models.PositiveIntegerField(default=0, blank=True)
    group = models.ForeignKey(\'tovar.Group\', on_delete = models.PROTECT, null=True)
    tags = models.ManyToManyField(\'tovar.Tag\')
         def _str_(self):
        if self.article is None:
            return \'{1} (---) {0}\'.format(self.title, self.pk)
        else:
            return \'{2} ({0}) {1}\'.format(self.article, self.title, self.pk)
              class Group(models.Model):
    title = models.CharField(max_length=128, unique=True)
    code = models.CharField(max_length=16, unique=True)
    skoro = models.NullBooleanField()
         @property
    def full_title(self):
        return \'({0}) {1}\'.format(self.code, self.title)
         def _str_(self):
        return self.full_title
      class Tag(models.Model):
    title = models.CharField(max_length=32, primary_key=True)
     def _str_(self):
        return self.title

[/apcode]

Решенный вопрос
Добавить комментарий