简体   繁体   中英

Django - FileField invalid literal for int() with base 10: 'media'

I'm making simple blog website in Django and I got this error: invalid literal for int() with base 10: 'media' . It's happnes when I added FileField to models.py in my blog application. Here is some code:

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', 'Draft'),
        ('publish', 'Public')
    )

    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

Here is part of 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")

And 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)

Thanks a lot for help !

blog/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'),
]

These pattern are consuming all requests to media files.

url(r'^', include('blog.urls'))  # in main urls.py

url(r'(?P<pk>[^/]+)', PostDetail.as_view(), name='post')  # in blogs/urls.py

When you go to http://127.0.0.1:8000/media/media_cdn/e1980c9642c03529db70a9c6060f247f.jpg , the url router tries to use that for a blog entry, which causes this error.

You should rewrite your url patterns so that this doesn't happen. If your blogs urls only consume numeric urls ( for example http://127.0.0.1:8000/1/ ), you can create a pattern for this.

url(r'^(?P<pk>\d+)/$', PostDetail.as_view(), name='post'),
url(r'^(?P<pk>\d+)/(?P<slug>[-\w]+)/$', PostDetail.as_view(), name='post_detail'),

Remember to use ^ and $ in your url patterns. See the official documentation for more examples and explanation of how url patterns and dispatching works. https://docs.djangoproject.com/en/1.11/topics/http/urls/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM