从Django管理面板上传图像到数据库

时间:2013-07-25 09:30:01

标签: django django-admin django-database django-models

这是我的model.py

from django.db import models

class Flower(models.Model):
    name = models.CharField(max_length = 30)
    price = models.IntegerField()
    image = models.ImageField(upload_to = 'static/media')
    def __unicode__(self):
        return self.name

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

    urlpatterns = patterns('',
        # Examples:
        url(r'^$', 'site1.views.home', name='home'),
        # url(r'^mysite1/', include('mysite1.foo.urls')),

        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

        # Uncomment the next line to enable the admin:
        url(r'^admin/', include(admin.site.urls)),
    )

admin.py

from django.contrib import admin
from site1.models import Flower

admin.site.register(Flower)

完整追溯

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1233.             'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __len__
  85.                 self._result_cache = list(self.iterator())
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in iterator
  291.         for row in compiler.results_iter():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  763.         for rows in self.execute_sql(MULTI):
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  818.         cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py" in execute
  40.             return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  344.             return Database.Cursor.execute(self, query, params)

Exception Type: DatabaseError at /admin/site1/flower/
Exception Value: no such column: site1_flower.image

我想从管理面板上传图片到我的网站数据库,所以我可以在任何我想要的地方查看这些图片。 但我遇到了一个错误:

DatabaseError at /admin/site1/flower/
no such column: site1_flower.image
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/site1/flower/
Django Version: 1.4.3
Exception Type: DatabaseError
Exception Value:    
no such column: site1_flower.image

我如何克服这个问题。

1 个答案:

答案 0 :(得分:0)

根据*异常值:没有这样的列:site1_flower.image * 我想,你已经在syncdb之后添加了图像字段。因此,db中不存在图像字段。 最简单的方法是删除Flower模型并再次运行syncdb。 或者您可以在数据库中手动创建此字段。 但最好的方法是使用django south