Django display



  • Good afternoon, trying to make a website on Django, I want to put it on the front page of the case with their preview, but Django's test server meets 404 errors each time. Django 1.9

    models.py

    from django.db import models
    

    class Case(models.Model):
    case_name = models.CharField(max_length=30)
    case_preview = models.ImageField(upload_to='images')

    def __str__(self):
        return self.case_name
    

    settings.py

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

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

    urls.py

    from django.conf.urls import patterns, include, url
    from django.conf.urls.static import static
    from django.contrib import admin
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    from django.conf import settings

    urlpatterns = patterns('',
    url(r'^landing/', include('landing.urls', namespace="landing")),
    url(r'^admin/', admin.site.urls),
    ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

    views.py

    from django.shortcuts import get_object_or_404, render
    from django.views import generic

    from .models import Case

    class IndexView(generic.ListView):
    template_name = 'landing/index.html'
    context_object_name = 'case_list'

    def get_queryset(self):
        return Case.objects.all()
    

    index.html

        {% if case_list %}
    <ul>
    {% for case in case_list %}
    <img src="{{ MEDIA_URL }}{{ case.case_preview.url }}"/>
    <li>{{ case.case_name }}</li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No cases are available.</p>
    {% endif %}

    I've already retrieved all the Jango forums and documentation, but I never found solutions.



  • Finally, I found a solution to the problem not to use the folder media in static.

    It was like:

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

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

    It's like,

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

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




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2