django - Uploaded images not showing, wrong path -


I have trouble displaying images uploaded from admin panel

  class articles (Models.Model): "News", displayed on the homepage to attract users "Meta: db_table = 'article' title = models.CharField (max_length = 64) headline = models.CharField (max_length = 255) content = HTMLField () image = models.ImageField (upload_to = 'article /', tap = true, empty = true) active = model Booleanfield () created_at = model.dateimfield () def __unicode __ (self): return self.title   

This url configuration is: django.conf.urls with import pattern, it contains, Url django.conf.urls.static imports from django.conf import settings static urlpatterns = pattern ('', #in some stuff) + static (settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Settings.py:

  PROJECT_DIR = os.path.dirname (__file__) MEDIA_ROOT = os.path.join (PROJECT_DIR, "media") MEDIA_URL = '/ media /'   

This view:

  def slider (requested): reference = context ({'article': Article.objects.order_by ('created - At ') [: 5]})  
  {article's article%} for%}   

and render (request,' article) / Slider HTM ', reference)

and Lieutenant; IMG src = "{{article.image.url}}" alt = "" />

I hope Django is expected to render http://127.0.0.1:8000/media/articles/slide-02.jpg but Now it's http://127.0.0.1:8000/media/slide-02.jpg . I have defined the upload_to = article / model class, so why the article.image.url attribute returns a path without this important directory?


Edit: Something wrong with my model does not recognize the upload_to directory:

  $ ./manage. Python Python 2.7.3 (default, 1 August 2012, 05:14:39) [GCC 4.6.3] For more information on linux2, type "help", "copyright", "credit" or "license" (interactive console ) & Gt; & Gt; & Gt; Articles from Import Models Articles & gt; & Gt; Article: On the official day, 20th April, 2010 & quot; Article: premiere of festival day in four days; & Lt; Article: Black Recording Launch & gt ;, & lt; Article: Deep Purple & gt; Country declared & gt ;, & lt; Article: Heavy Innovation, & lt; Article: Festival d'Ette, Quebec City & gt;]> gt; & Gt; & Gt; Article.objects.all (). Get (pk = 1) & lt; Article: Black Recording Launch & gt; & Gt; & Gt; & Gt; Article.objects.all (). Get (pk = 1) .image & lt; ImageFieldFile: slide-03.jpg & gt; & Gt; & Gt; & Gt; Article.objects.all (). Get (pk = 1) .image.path u '/ var / www / django / djninja / djninja / media / slide-03.jpg' & gt; & Gt; & Gt; Articles.All (). Get (pk = 1) .image.url '/media/slide-03.jpg'   

Again, this should be the media / article /slide-03.jpg instead of media / slide-033.jpg . So I think all the routing / templates are fine, there is something wrong with the model.

The solution above is absolutely fine. My problem was loaded in inappropriate configurable fixtures and databases written in JSON. I thought you pass the article directory as upload_to kwarg, it will be loaded dynamically. it is not It is used only during saving image and it is ignored during loading of image from the database. Thus, if I

  was "image": "slide.jpg"   

I changed it:

 < Code> "image": "article / slide.jpg"   

and it worked. In fact, it used to work all the time, but I miss a note in Dōjōgo official documents.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -