🔖 Day2 - Staticfiles, Media, Dynamic URL, order_by

2018 - 06 - 26
🔖 Day2 - Staticfiles, Media, Dynamic URL, order_by
1. [STATIC_URL] 架設網站少不免用到其他scripts,例如:JS, jQuery, CSS 等。這些為網站添上風格的, 當然每一頁都用得到,但亦不會每一頁都重寫一遍,而是把編碼儲存於某些特定子folders內。 有別於以往使用 ../ 或 ./ 等路徑,Django有static這個功能。 假設要把編寫好的CSS檔案儲存: 確認settings.py - INSTALLED APPSdjango.contrib.staticfiles這一項預設apps。 然後在settings.py最下方,define static的路徑:
/* tied to particular apps */
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

/* Tells Django how to present the path to static folder in URL */
STATIC_URL = '/static/'

/* not tied to specific apps, e.g. css or js files for all apps */

STATICFILES_DIRS = [                                                   
    os.path.join(BASE_DIR,'static')
]

要注意staticfiles_dirs 不可以跟static_root一樣。
2. [Load static into templates] 把CSS file 儲存於static/css/ 內。 當要在HTML中使用時,要先在HTML最頂加入 {% load static %} 接著在呼叫CSS的路徑時,改成: <a href=“{% static 'css/xxx.css' %}”> 最後在terminal命令collectstatic: python manage.py collectstatic
3. [MEDIA_URL] 假設在models.py當中有些data是用到upload, e.g. files, photos等,可以在models.py - class 裡面define: e.g.
class Post(models.Model):
    ...
    ...
    photo_upload = ImageField(null=True, blank=True) / FileField(null=True, blank=True)

null & blank =True to prevent exceptions without default values 因為要upload media, 需要在settings.py declare media路徑:
MEDIA_ROOT = os.path.join(BASE_DIR, “media”)
MEDIA_URL = “/media/“


4. [Setup url] 同時在urls.py 加入:
from django.conf import settings
from django.conf.urls.static import static

urlpatterns =[...] 
urlpatterns += static(settings.MEDIA_URL, documents_root=settings.MEDIA_ROOT)


5. [Migrate new data and collectstatic] 因為這次 step3 + step4分別在models.py新增了data-type; 更改了MEDIA_URL; 所以要在terminal makemigrations, migrate, collectstatic
>python manage.py makemigrations
>python manage.py migrate
>python manage.py collectstatic


6. [Recalling uploaded photo] 假設需要在HTML引用uploaded media, 因為己經在urlpatterns defined, 所以HTML內的引用路徑為: <href=“{{blog.photo_upload.url}}">
7. [DynamicURL] 由於class的每一個element都有自己的primary key(pk), 所以可以利用pk呼叫各element的網址。 首先在urls.py - urlpatterns增加一行: /* name='postpage' 是為日後在HTML內部呼叫時用的,下面會再提及 */
url(r'blog/(int: key)/', postpage, name='postpage')

然後到apps/views.py新增function 'postpage':
def postpage(request, key):
    post = Post.objects.get(pk=key)
    return render(request, page.html, {'post': post})


8. [Using "name" to call a url] 假如要在HTML內部呼叫某一element的頁面,可以用step7 declared的url name: e.g. {% url 'name' key=post.pk %}
9. [Order_by] 因為使用python for loop列出所有posts時,會由Posts[0], Posts[1], Posts[2],..., Posts[n] 這個次序去完成, 所以最好的方法是用在views.pydeclare function時,把post_list 改成descending list: e.g.
from .models import Post
from django.shortcuts import render

def app_index(request):
    post_list = Post.objects.all().order_by('-time')
    return render(request, 'app_index.html', {'post_list': post_list})

The '-' sign means descending order, i.e. from newest to oldest "time" value.

Comments

Kavie
Bold Text Here
Jan. 25, 2018, 10:33 p.m.

New Comment

Please Login to comment