

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Thu, 01 Feb 2024 02:13:17 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>Django &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Django 教學2: 增加新的app頁面</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/django-2-skeleton_website/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/django-2-skeleton_website/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 03 Feb 2023 06:17:33 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4223</guid>

					<description><![CDATA[安裝 Django 一下子就完成了, 接下來要在...]]></description>
										<content:encoded><![CDATA[
<p>安裝 Django 一下子就完成了, 接下來要在 Django 裡增加新的頁面. 這篇文章的資料來源:<br>https://developer.mozilla.org/zh-TW/docs/Learn/Server-side/Django/skeleton_website</p>



<p>首先, 創立專案:</p>



<pre class="wp-block-code"><code>mkdir locallibrary<br>cd locallibrary<br>django-admin startproject locallibrary<br>cd locallibrary</code></pre>



<h2 class="wp-block-heading">建立 catalog app:</h2>



<p>Linux/macOS 指令:</p>



<pre class="wp-block-code"><code>python3 manage.py startapp catalog</code></pre>



<p>Windows 指令:</p>



<pre class="wp-block-code"><code>py -3 manage.py startapp catalog</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">註冊 catalog app</h2>



<p>編輯檔案: locallibrary/locallibrary/settings.py </p>



<p>每次 startapp 之, 請到 settings.py 增加下面紅字的部份:</p>



<pre class="wp-block-code"><code>INSTALLED_APPS = &#91;
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">'catalog.apps.CatalogConfig'</mark>,
]</code></pre>



<p>說明: 上面這行, 其實就是出現在檔案: /locallibrary/catalog/apps.py</p>



<p>如果之後有修改 app 裡的內容, 記得用下面指令同步 Django  的監控:</p>



<p>Linus / macOS:</p>



<pre class="wp-block-code"><code>python3 manage.py makemigrations catalog</code></pre>



<p>Windows</p>



<pre class="wp-block-code"><code>py -3 manage.py makemigrations catalog</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">修改 settings.py 裡其他的設置</h2>



<p>檔案: /locallibrary/locallibrary/settings.py</p>



<p>LANGUAGE_CODE = &#8216;zh-Hant&#8217;<br>TIME_ZONE = &#8216;Asia/Taipei&#8217;</p>



<p><code>DEBUG</code>. 這個會在 debug 日誌裡輸出錯誤信息，而不是輸入 H​​TTP 的返回碼。在生產環境中，它應設置為 false，因為輸出的錯誤信息，會幫助想要攻擊網站的人。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">修改 URL 映射器（urls.py）</h2>



<p>檔案: /locallibrary/locallibrary/urls.py 增加下面紅字內容:</p>



<pre class="wp-block-code"><code>from django.contrib import admin
from django.urls import path

urlpatterns = &#91;
    path('admin/', admin.site.urls),
    <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">path('catalog/', include('catalog.urls')),</mark>
]</code></pre>



<p>說明: 遇到網址為 catalog/ 時, 轉發請求到模塊 catalog.urls（/locallibrary/catalog/urls.py 的文件）。</p>



<p>複製上面的 /locallibrary/locallibrary/urls.py 檔案到: /locallibrary/catalog/urls.py , 並修改內容為下:</p>



<pre class="wp-block-code"><code>from django.urls import path
from . import views

urlpatterns = &#91;
    path('', views.index, name="index"),
]
</code></pre>



<p>說明: 遇到 /catalog 時, 到 views object 裡, 存取 index method.</p>



<p>修改 /locallibrary/catalog/views.py  內容為下:</p>



<pre class="wp-block-code"><code>from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def index(request):
	return HttpResponse("Hello, catelog view.")</code></pre>



<p><code>path()</code>&nbsp;函數還指定一個<code>name</code>參數，它是此特定 URL 映射的唯一標識符。 您可以使用該名稱來“反向”映射器，即，動態創建指向映射器旨在處理的資源的 URL。 例如，通過在模板中添加以下鏈接，我們可以使用 name 參數從任何其他頁面鏈接到我們的主頁：</p>



<pre class="wp-block-code"><code>&lt;a href="{% url 'index' %}"&gt;Home&lt;/a&gt;.
</code></pre>



<p>Copy to Clipboard</p>



<p><strong>備註：</strong>&nbsp;我們可以對上面的鏈接進行硬編碼 (例如<code>&lt;a href="/catalog/"&gt;Home&lt;/a&gt;</code>), 但是如果我們更改主頁的模式 (例如更改為&nbsp;<code>/catalog/index</code>) 則模板將不再 正確鏈接。 使用反向 URL 映射更加靈活和健壯！</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>執行網址：<br><a href="http://127.0.0.1:8000/catalog/">http://127.0.0.1:8000/catalog/</a></p>



<p>執行畫面：</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="601" height="250" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-15_04_02-127.0.0.1_8000_catalog_.jpg" alt="" class="wp-image-4229"/></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/django-2-skeleton_website/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>在 Python 虛擬環境中使用 Django</title>
		<link>https://stackoverflow.max-everyday.com/2023/02/python-venv-django/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/02/python-venv-django/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 03 Feb 2023 02:32:55 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4214</guid>

					<description><![CDATA[不管是什麼python 的程式, 在虛擬環境下執...]]></description>
										<content:encoded><![CDATA[
<p>不管是什麼python 的程式, 在虛擬環境下執行, 往往可以避開一些環境共用造成的衝突問題, 別以為使用最新版本的套件就不會遇到, 因為往往就是&#8221;太新版&#8221; 或&#8221;太舊版&#8221; 的第三方套件會衝突。 </p>



<p>virtualenvwrapper 官方說明文件：<br><a href="https://virtualenvwrapper.readthedocs.io/en/latest/install.html">https://virtualenvwrapper.readthedocs.io/en/latest/install.html</a></p>



<p>Github 專案：<br><a href="https://github.com/python-virtualenvwrapper/virtualenvwrapper">https://github.com/python-virtualenvwrapper/virtualenvwrapper</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading" id="ubuntu_虛擬環境設置">Ubuntu  虛擬環境設置</h4>



<p>安裝 Python 和 pip 之後，你可以安裝 virtualenvwrapper（包括 virtualenv）。</p>



<p>使用 pip3 安裝該工具：</p>



<pre class="wp-block-code"><code>sudo pip3 install virtualenv virtualenvwrapper</code></pre>



<p>或:</p>



<pre class="wp-block-code"><code>python3 -m pip install virtualenv virtualenvwrapper</code></pre>



<p></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>目前 virtualenvwrapper 支援 bash及 zsh.</p>



<p>增加下面三行指令到shell startup file (<code>例如: .bashrc</code>,&nbsp;<code>.profile</code>.) &nbsp;:</p>



<pre class="wp-block-code"><code>export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh</code></pre>



<p>編輯好了之後, 重新載入啟始的shell 環環. <br>After editing it, reload the startup file.</p>



<p> (e.g., run&nbsp;<code>source&nbsp;~/.bashrc</code>).</p>



<p>上面這一段，我沒有去使用，因為想切換virtualenv，再切換就好了。在 macOS 裡，透過 python -m pip 指令安裝完後，並不會有 /usr/local/bin/virtualenvwrapper.sh 這個 shell script 檔。但是有 virtualenv 這個指令。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading" id="macos_x_虛擬環境設置">macOS X 虛擬環境設置</h4>



<p>在 macOS X 上設置 virtualenvwrapper 與在 Ubuntu 上幾乎完全相同。</p>



<p>使用 pip 安裝 virtualenvwrapper（並捆綁 virtualenv）:</p>



<pre class="wp-block-code"><code>sudo pip3 install virtualenv virtualenvwrapper</code></pre>



<p>或:</p>



<pre class="wp-block-code"><code>python3 -m pip install virtualenv virtualenvwrapper</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading" id="windows_10_虛擬環境設置">Windows 10 虛擬環境設置</h4>



<p>安裝virtualenvwrapper-win比設置 virtualenvwrapper 更簡單，因為您不需要配置工具存放虛擬環境信息的位置。您需要做的就是，在命令提示符中運行以下命令：</p>



<pre class="wp-block-code"><code>pip3 install virtualenvwrapper-win</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">使用虛擬環境</h2>



<p>這些是您經常使用的命令：</p>



<ul class="wp-block-list">
<li>deactivate — 退出當前的 Python 虛擬環境</li>



<li>workon — 列出可用的虛擬環境</li>



<li>workon name_of_environment — 啟用指定的 Python 虛擬環境</li>



<li>mkvirtualenv name_of_environment — 建立指定的環境</li>



<li>rmvirtualenv name_of_environment — 刪除指定的環境</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">建立新的虛擬環境</h2>



<p>使用的是 mkvirtualenv + 環境名稱。</p>



<p>在 Windows 的操作：</p>



<pre class="wp-block-code"><code>mkvirtualenv my_django_env</code></pre>



<p></p>



<p>在 macOS 的操作：</p>



<p>使用virtualenv 指令，會將環境建立在當前資料夾底下，可以使用 &#8220;專案名稱_env&#8221; 來命名，會簡單一點：</p>



<pre class="wp-block-code"><code>virtualenv my_django_env</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">啟用虛擬環境</h2>



<p>Window 啟用虛擬環境的指令:</p>



<pre class="wp-block-code"><code>workon my_django_env</code></pre>



<p>實際測試, 使用 mkvirtualenv 建立目錄的同時也會啟用該新的目錄。</p>



<p></p>



<p>macOS 啟用虛擬環境的指令:</p>



<pre class="wp-block-code"><code>cd my_django_env
. ./bin/activate</code></pre>



<figure class="wp-block-image"><img decoding="async" width="1120" height="198" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/Screenshot-2017-02-23-21.22.36.jpg" alt="" class="wp-image-249"/></figure>



<p>滿有趣的，提示字元前多了一個(venv)</p>



<p>要退出，就是直接下&nbsp;deactivate 即可, 不用加 ./bin/ , 而且 ./bin/ 目錄裡也沒有 deactivate 的檔案。</p>



<p>只有第1次需要下 virtualenv + 目錄，之後要進去已創立的虛擬環境，就是進去資料夾裡執行 . ./bin/activiate 就OK了。</p>



<p></p>



<p>要退出，就是下 deactivate 指令就可以了。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">虛擬環境之中安裝 Django</h2>



<p>在虛擬環境之中安裝 Django, Linux/macOS 請使用:</p>



<pre class="wp-block-code"><code>pip3 install django</code></pre>



<p>Windows 在虛擬環境下, 請使用:</p>



<pre class="wp-block-code"><code>py -3 -m pip install django</code></pre>



<p>除了上面的特殊用法之外，使用一般的 python -m pip list 就會看到只剩下3個 package:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="547" height="388" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2024/02/ConEmu64_2024-02-01_10-06_9o.png?v=1706753477" alt="" class="wp-image-5407"/></figure>
</div>


<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Django念法</h2>



<p>Django 發音為JanGo，字母&#8221;D &#8220;是不發聲的.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">取得Django安裝的版本號:</h2>



<p>Linux/macOS 使用:</p>



<pre class="wp-block-code"><code>python3 -m django --version</code></pre>



<p>Windows 使用:</p>



<pre class="wp-block-code"><code>py -3 -m django --version</code></pre>



<p>如果Windows 使用 python3 -m django &#8211;version 傳回值是空字串, 感覺很奇怪!</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">測試你的安裝</h2>



<pre class="wp-block-code"><code>mkdir django_test<br>cd django_test<br>django-admin startproject mytestsite<br>cd mytestsite</code></pre>



<p>使用manage.py runserver 命令， 執行 Web 服務器, Linux/macOS 請執行:</p>



<pre class="wp-block-code"><code>python3 manage.py runserver</code></pre>



<p>Windows 請執行:</p>



<pre class="wp-block-code"><code>py -3 manage.py runserver</code></pre>



<p>Console 執行畫面:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="339" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_25_24-C__Windows_system32_cmd.exe-py-3-manage.py-runserver-1024x339.jpg?v=1675401990" alt="" class="wp-image-4220" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_25_24-C__Windows_system32_cmd.exe-py-3-manage.py-runserver-1024x339.jpg?v=1675401990 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_25_24-C__Windows_system32_cmd.exe-py-3-manage.py-runserver-600x199.jpg?v=1675401990 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_25_24-C__Windows_system32_cmd.exe-py-3-manage.py-runserver-768x255.jpg?v=1675401990 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_25_24-C__Windows_system32_cmd.exe-py-3-manage.py-runserver.jpg?v=1675401990 1445w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Browser 執行畫面:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="548" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_24_37-C__Max_NARlabs_django_nar_workflow_workflowtest_manage.py-Sublime-Text-LICENS-1024x548.jpg?v=1675402001" alt="" class="wp-image-4221" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_24_37-C__Max_NARlabs_django_nar_workflow_workflowtest_manage.py-Sublime-Text-LICENS-1024x548.jpg?v=1675402001 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_24_37-C__Max_NARlabs_django_nar_workflow_workflowtest_manage.py-Sublime-Text-LICENS-600x321.jpg?v=1675402001 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_24_37-C__Max_NARlabs_django_nar_workflow_workflowtest_manage.py-Sublime-Text-LICENS-768x411.jpg?v=1675402001 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_24_37-C__Max_NARlabs_django_nar_workflow_workflowtest_manage.py-Sublime-Text-LICENS-1536x822.jpg?v=1675402001 1536w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/02/2023-02-03-13_24_37-C__Max_NARlabs_django_nar_workflow_workflowtest_manage.py-Sublime-Text-LICENS.jpg?v=1675402001 1774w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">相關文章</h2>



<p>在 Mac OS 用 PyInstaller 打包 python<br><a href="https://stackoverflow.max-everyday.com/2017/02/mac-pyinstaller/">https://stackoverflow.max-everyday.com/2017/02/mac-pyinstaller/</a></p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/02/python-venv-django/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
