Tuesday, September 26, 2017

Re: CharField vs ImageField for Logo

Got it. Thanks a lot!!

On Tue, Sep 26, 2017 at 3:33 PM, Andréas Kühne <andreas.kuhne@hypercode.se> wrote:
Hi, 

great that you are getting how the media settings work.

If you look at this page : https://docs.djangoproject.com/en/1.11/howto/static-files/ it explains what static files are and how they work.  Basically, you use the static files template tags in your html templates and then you can use the same solution in development as in production. It's mainly good if you for example are serving your static files from your computer in development, but from a CDN i production (we do that on a project I wrote) and you then don't have to change anything but the settings file in production.

The reason you need to add the configuration to your settings file is because the django runserver process doesn't know where to serve your files from otherwise. There are no "reasonable" defaults for those settings.

Hope that helps!

Andréas

2017-09-26 0:40 GMT+02:00 tango ward <tangoward15@gmail.com>:
Hi Andreas,


I was able to display the images but I dont know how they work. I checked some online resources and found out that I need to add these in my urls.py

from django.conf import settings
from django.conf.urls.static import static

and at the bottom of it, I added

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


I also saw a urlpattern for STATIC_URL and STATIC_ROOT.

Would you have a minute or two to help me understand these codes?

I would really appreciate it.


Thanks

On Tue, Sep 26, 2017 at 5:23 AM, tango ward <tangoward15@gmail.com> wrote:
Hi Andreas,


Really appreciate your guidance on this.

I am having trouble understanding this:

3: All that will be stored in your database is a path to the file (relative to MEDIA_ROOT). You'll most likely want to use the convenience url attribute provided by Django. For example, if your ImageField is called mug_shot, you can get the absolute path to your image in a template with {{ object.mug_shot.url }}.


I tried applying the object.logo.url but the logo still doesn't display. Also where did the object and url came from?

On Tue, Sep 26, 2017 at 3:39 AM, Andréas Kühne <andreas.kuhne@hypercode.se> wrote:
Hi,

I think you've made a lot of progress! 

The only thing I think you are missing now is that you should be using the following in your template:
<img src="{{ team.logo.url }}">


Regards,

Andréas

2017-09-25 20:38 GMT+02:00 tango ward <tangoward15@gmail.com>:
Hi Andréas,


Thank you for the response.

I added these lines in my settings.py

MEDIA_DIR = os.path.join(BASE_DIR, 'media')

# Media

MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/

Then I changed my logo to models.ImageField(upload_to='team_logo'). I also created a 'media' folder inside my project folder which team_logo is a subfolder in it. Tried running the codes again but the logo still wont show.


{% block body_block %}
   
    <p></p>
    <div class="row">
        {% for team in teams %}
        <div class="col-lg-3 col-xs-6 thumbnail">
           
            <img src="{{ team.logo }}">
             <p></p>

            <label>{{ team.name }}</label>
        </div>   
        {% endfor %}
    </div>
   

{% endblock %}


Is there any setting that I missed in media for media?


Thanks,
Jarvis

On Mon, Sep 25, 2017 at 9:10 PM, Andréas Kühne <andreas.kuhne@hypercode.se> wrote:
Hi,

There are a couple of things to think about here.

First of all - just because you put an item on your computer doesn't mean that the icon can be served. For example, if you are running windows and you enter 'C:\pictures\icon.jpg' as the source for the icon, your server won't be able to find it, because the reference 'C:\pictures\icon.jpg' doesn't mean anything for the web browser. The reason the image links you are inputting work is because they probably contain all information, ie http://www.example.com/pictures/icon.jpg.

So you can get that sorted and everything should work. HOWEVER, I really think you should use the media storage functionality of Django for this. Checkout : https://docs.djangoproject.com/en/1.11/topics/files/.

If you add the settings required (MEDIA_ROOT in the settings file) and then change you logo from a CharField to an ImageField AND upload your file via django admin, you should be able to show your file.

Regards,

Andréas

2017-09-25 14:38 GMT+02:00 tango ward <tangoward15@gmail.com>:

Hi guys, I am new to django currently stuck in these two.

I am writing my pet project which will display a Team name and their logo. I used these lines for my Team class in mode.spy


class Team(models.Model):
    name = models.CharField(max_length=150)
    logo = models.CharField(null=True, max_length=1000)


    def __str__(self):
        return self.name


Problem is, I can't load the images to my html file My logo images are currently stored in my computer. The images will load properly if I grab an image link online and paste the image location to the Logo field in Admin but if I used the absolute path of the images in my computer, the images wont load. When I tried to inspect the page, I am getting "Image could not load".

Any tips please?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA6wQLJ6kjsd80%2BcmFOZhPB5C_1Ugnu%3Ds51aisfNgK%3DambvJUg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCfrBi-0XwcHQLJHWRcfbZHDuXR-ciBx6xB1TR_AR82h%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA6wQLLtaj5p1WPCZ3ozZFMFHyqjg%2B1AoDLzXq8qY79JNwg%3DwQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCcJ-k%3D04nZjF0fY3vqwsbXp4gjMPW6cOZtZ5n%3DfXrYG6w%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA6wQLLsf_VyRizj%2BsXYo7kJSgFD%2BnGrpctnkqic7JU%3DaKO35g%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCf7NNW5OwfsA_an_v3WsnFE5vPfm-9qGyX55QiKNeLHmw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA6wQL%2Btkpy33zWXNm2geDakd_sfsmYyOTr4YQpxheXojRfS2A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment