我正在Django制作简单的博客网站,我收到了这个错误:invalid literal for int() with base 10: 'media'
。当我在我的博客应用程序中将modelsField添加到models.py时,它很高兴。这是一些代码:
models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Roboczy'),
('publish', 'Publiczny')
)
author = models.ForeignKey(User)
title = models.CharField(max_length=140)
slug = models.SlugField(max_length=140)
image = models.FileField(blank=False, null=False, upload_to='media_cdn')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
class Meta:
ordering = ['-publish']
def __str__(self):
return self.title
以下是settings.py的一部分:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media_cdn")
和urls.py
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('blog.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
非常感谢您的帮助!
编辑: 全程引用:
环境:
请求方法:GET请求URL: http://127.0.0.1:8000/media/media_cdn/e1980c9642c03529db70a9c6060f247f.jpg
Django版本:1.11.4 Python版本:3.6.2已安装的应用程序: ['pagedown','markdown_deux','blog','django.contrib.admin', 'django.contrib.auth','django.contrib.contenttypes', 'django.contrib.sessions','django.contrib.messages', 'django.contrib.staticfiles']已安装的中间件: [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
回溯:
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/core/handlers/exception.py” 在内心 41. response = get_response(request)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/core/handlers/base.py” 在_get_response中 187. response = self.process_exception_by_middleware(e,request)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/core/handlers/base.py” 在_get_response中 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/views/generic/base.py” 在视野中 68.返回self.dispatch(request,* args,** kwargs)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/views/generic/base.py” 在发货 88.返回处理程序(request,* args,** kwargs)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/views/generic/detail.py” 在得到 115. self.object = self.get_object()
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/views/generic/detail.py” 在get_object中 38. queryset = queryset.filter(pk = pk)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/query.py” 在过滤器中 784. return self._filter_or_exclude(False,* args,** kwargs)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/query.py” 在_filter_or_exclude中 802. clone.query.add_q(Q(* args,** kwargs))
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/sql/query.py” 在add_q中 1250. clause,_ = self._add_q(q_object,self.used_aliases)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/sql/query.py” 在_add_q中 1276. allow_joins = allow_joins,split_subq = split_subq,
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/sql/query.py” 在build_filter中 1210. condition = self.build_lookup(lookups,col,value)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/sql/query.py” 在build_lookup中 1104. return final_lookup(lhs,rhs)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/lookups.py” 在 init 24. self.rhs = self.get_prep_lookup()
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/lookups.py” 在get_prep_lookup中 74. return self.lhs.output_field.get_prep_value(self.rhs)
文件 “/home/zamknijryjx/Pictures/test/env/lib/python3.6/site-packages/django/db/models/fields/init.py” 在get_prep_value中 966. return int(value)
异常类型:ValueError at /media/media_cdn/e1980c9642c03529db70a9c6060f247f.jpg异常值: int()的基数为10的无效文字:'media'
博客/ urls.py
from django.contrib.auth.urls import url
from .views import PostList, PostDetail
urlpatterns = [
url(r'^$', PostList.as_view(), name='blog'),
url(r'(?P<pk>[^/]+)', PostDetail.as_view(), name='post'),
url(r'(?P<pk>[^/]+)/(?P<slug>[-\w]+)$',
PostDetail.as_view(), name='post_detail'),
]
答案 0 :(得分:2)
这些模式正在消耗对媒体文件的所有请求。
url(r'^', include('blog.urls')) # in main urls.py
url(r'(?P<pk>[^/]+)', PostDetail.as_view(), name='post') # in blogs/urls.py
当您转到http://127.0.0.1:8000/media/media_cdn/e1980c9642c03529db70a9c6060f247f.jpg
时,网址路由器会尝试将其用于博客条目,从而导致此错误。
您应该重写您的网址模式,以便不会发生这种情况。如果您的博客网址仅使用数字网址(例如http://127.0.0.1:8000/1/
),则可以为此创建模式。
url(r'^(?P<pk>\d+)/$', PostDetail.as_view(), name='post'),
url(r'^(?P<pk>\d+)/(?P<slug>[-\w]+)/$', PostDetail.as_view(), name='post_detail'),
请记住在您的网址格式中使用^
和$
。
有关url模式和调度如何工作的更多示例和说明,请参阅官方文档。
Hadoop Pig - Removing csv header