在Eclipse中創(chuàng)建Django項目

??在以前的分享中,我們是在命令行模式下創(chuàng)建Django項目的,那么,如何在IDE中使用Django呢?
??本文將介紹如何在Eclipse中創(chuàng)建Django項目。
??首先,新建Django項目mysite,如下圖:

新建Django項目

新建Django項目mysite

注意上圖中的劃紅線部分,應(yīng)該選擇“Add project directory to the PYTHONPATH”,之后一直點next和finish即可,建好的mysite項目如下圖:


mysite項目

??在mysite模塊下,新建views.py,代碼如下:

from django.http import HttpResponse

def output(request):
    title = "<h1>When You Are Old</h1>"
    author = "<h2>William Butler Yeats</h2>"
    content = """
                 When you are old and grey and full of sleep,<br/>
                 And nodding by the fire, take down this book,<br/>
                 And slowly read, and dream of the soft look<br/>
                 Your eyes had once, and of their shadows deep;<br/>
                 How many loved your moments of glad grace,<br/>
                 And loved your beauty with love false or true,<br/>
                 But one man loved the pilgrim soul in you,<br/>
                 And loved the sorrows of your changing face;<br/>
                 And bending down beside the glowing bars,<br/>
                 Murmur, a little sadly, how love fled<br/>
                 And paced upon the mountains overhead<br/>
                 And hid his face amid a crowd of stars.<br/>
               """
    return HttpResponse([title, author, content])

??在urls.py增加url路徑:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^/pre>,'mysite.views.output'),
]

??最后,我們需要在manage.py中,修改代碼,將execute_from_command_line(sys.argv)命令改為自己做需要的命令,如下代碼:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
    from django.core.management import execute_from_command_line
    execute_from_command_line(['manage.py','runserver','0.0.0.0:8000'])

保存,并運行,在Eclipse中運行結(jié)果如下:


Eclipse中運行結(jié)果

最后,我們在本地瀏覽器中輸入localhost:8000即可,顯示如下圖:


瀏覽器中顯示結(jié)果

??這樣,我們就成功地在Eclipse中創(chuàng)建Django項目并順利運行了,簡單又方便,不需要再在命令行模式下去操作。

??那么,如何新建Django app呢?我們只需在原項目下,新建PyDev Package,這就是一個Django app.

??那么,又該如何實現(xiàn)python manage.py makemigrations和python mange.py migrate呢?和上面的操作一樣,我們只需要在manage.py新增代碼:

execute_from_command_line('manage.py','makemigrations')
execute_from_command_line('manage.py','migrate')</pre>

??本次分享到此結(jié)束,歡迎大家交流~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容