當前位置:首頁 » 網站資訊 » django怎麼導入網站

django怎麼導入網站

發布時間: 2023-05-15 12:40:13

怎麼將Django網站 上傳到空間里

一般的空間不支持都是支持.NET或php開發的網站。
python開發的網站要專門租用伺服器才行,當然這樣做成本就太高啦,如果你只是用django做的個人小站,可以上傳到陵鎮明網路或新浪旅晌的雲環境,這是最靠尺告譜的解決方案啦。
網路搜索一下「BAE」和「SAE」,去了解一下。
在上傳過程中遇到了不明白的地方,可以通過私信或追問來繼續詢問我。

希望對你有所幫助~

如何在阿里雲上部署django網站

Django的部署建議放在Linux系統上。比較易於新手入門的Linux伺服器發布版應該是Ubuntu,aliyun上目前最新版本應該是16.04,建議64位。

如果你是新網站,沒有多少訪問量,建議1核1內存或者2G內存,1-2M的寬頻就足夠了。最近雙十一還有活動。

部署,安裝Nginx:

aptinstallnginx

不知道你的Django項目是python2還是Python3,如果選用uwsgi部署方式,除了uwsig需要裝一個uwsgi-plugin-python或者uwsgi-plugin-python3

剩下的看官方文檔的uwsgi部署部分基本就ok了,因為全寫實在太長了,還有www.ini的uwsgi配置文件。

⑶ django中怎麼在一個網頁向另一個網頁傳遞參數

獲取字典中某彎姿耐個key的值有2種冊鏈方式:
print user.get('username')11
print user['username']11

他們到底有什麼埋春區別呢?
我來常識列印一個沒有的key,比如
print user.get('kk')11

列印結果為:None
print user['kk']11

頁面會報錯

⑷ 在pycharm導入django項目

有時候,我們需要在多個地方編輯項目,磨改這猜鏈就需要在pycharm中導入django項目。首先要做的,當然是使瞎兆判用virtualenvwrapper來搭建虛擬環境,並且將項目中的requestrements.txt文件中的相關依賴安裝好。
接下來要做的是:

在這里我們需要設置一下django項目的目錄與settings文件的位置,以及啟動文件的路徑

以上就是項目的導入了,試試debug吧!!

⑸ django1.7怎麼導入httpresponse

from django.http import HttpResponse

用法蠢物腔
return HttpResponse('success')#寫入帶衫相應螞碧的數據即可

⑹ 怎麼用python架設一個網站

你可以使用python的django來架設網站,步驟如下:
Django的安裝運行環境:Windows vista, python2.7
python安裝路徑:C:\Python27
從 https://www.djangoproject.com/ 下載django安裝包。
解壓後,進入django目錄,運行 python setup.py install,啟動安裝。
Django被安裝在 C:\Python27\Lib\site-packages
第一個工程的創建
生成工程框架:
c:\test\mysite>python C:\Python27\Lib\site-packages\django\bin\django-admin.py startproject mysite1

運行開發伺服器:
python manage.py runserver
在瀏覽器中,訪問 http://127.0.0.1:8000/,看到 「Welcome to Django」 的提示。

如果解決了您的問題請採納!
如果未解決請繼續追問!

⑺ Windows10使用IIS部署Django項目

相對於linux系統來說,Windows10下進行django項目部署要簡單許多,假設已搭建python環境,以及django項目已進行靜態資源的收集(執寬陪行了 python manage.py collectstatic ),那麼就可以開始以下操作了。

以管理員打開cmd命令安裝:

再次輸入命令:

會生成陵巧廳一個路徑,將它保存下來

從剛剛獲得的路徑中復制一份wfastcgi.py文件到項目根目錄中

在根目錄中新建一個web.config文件,內容如下:

打開管理工具,進入IIS管理器,添加網站:

添加完畢後即可訪問網站!

在IIS管尺隱理器中,進入應用程序池

訪問頁面或單獨訪問網站,後台出現400錯誤,這可能是因為沒有給網站許可權的原因

參考: https://blog.csdn.net/kiualen/article/details/93882956

⑻ python能做網站嗎

python可以做網站,Python有很多優秀的網站框架,可以非常快速的建一個網站。比如django之類畢肢的框架。

Django、TurboGears、Eurasia、UliWeb等:突出的共同特色有:

有內置的ORM模塊支持資料庫的對象化操作;

有內置的事務性功能支持(比如說登錄認證);

有高級的模板系統,支持復知數岩雜的頁面組合,有的甚至有內置搭御的Ajax頁面動態效果支持。

使用django框架建站的步驟:

1、導入django包

可直接在pycharm下載,或者pip/easy_install

2、設置環境變數

path添加C:Python27Libsite-packagesdjangobin;C:Python27Scripts

3、新建一個工程

D:>django-admin.pystartprojectmysite

4、工程下新建一個app

D:mysite>pythonmanage.pystartappblog

5、初始化admin後台資料庫

D:mysite>pythonmanage.pymigrate

6、啟動服務

D:mysite>pythonmanage.pyrunserver

⑼ 如何在阿里雲上部署django網站

概述與目的

此帖介紹了如橋胡何(利用 Ubuntu 操作系統)在阿里雲上啟動和部署 Django 應用程序。這兩個流程的詳細步驟說明如下所述:
● 使用阿里雲管理控制台啟動您的第一個 Linux 實例
● 在此實例上安裝和部署 Django 應用程序

關於 Django

Django 是一個用 Python 語言寫的開源 Web 框架,可幫助開發人員在構想形成後僅數小時內啟動 Web 應用程序。它遵循模型視圖模板 (MVT) 來構建應用程序,這可降低 Web 開發的復雜性,同時可讓開發人員集中精力編寫應用程序。它為網站地圖、內容管理、用戶鑒權、RSS 提要及其他任務提供開箱即用的設置。一些高流量網站使用 Django 是因為它能夠快速、靈活地進行調整,從而可滿足流量波動高峰期的需求。

前提條件

1. 為 Python 應用程序安裝 virtualenv 和 virtualenv wrapper,目的在於為 Python 項目創建獨立的環境。
● 安裝 pip

sudo apt-get install python-pip
● 安裝 virtualenv
sudo pip install virtualenv
● 創建目錄以存儲您的 virtualenv
mkdir ~/.virtualenvs
● 安裝 virtualenwrapper
sudo pip install virtualenvwrapper
● 將 WORKON_HOME 設置為您的 virtualenv 目錄
導出 WORKON_HOME=~/.virtualenvs
● 將 WORKON_HOME 設置為您的 virtualenv 目錄
導出 WORKON_HOME=~/.virtualenvs
● 將virtualenvwrapper.sh添加到.bashrc
將此行添加到 ~/.bashrc 的末尾,以便載入 virtualenvwrapper 命令。/usr/local/bin/virtualenvwrapper.sh
退出,然後重新打開您的 shell,或者使用.bashrc or source ~/.bashrc命令重新載入 .bashrc,然後一切准備就緒。

2. 安裝 git。

apt-get install git

3. 安裝 Nginx 作為 Web 伺服器,這樣您便可在其之後運行您的應用程序。

Sudo apt-get install nginx

啟動您的第一個 Linux 實例

接下來向您介紹使用阿里雲管理控制台運行 ECS 實例的相關步驟。

1. 登錄到您的帳戶,然後導航到「產品與服務」部分下的「雲伺服器」。單擊側邊欄菜單中的概覽。這會顯示運行中的實例列表。單擊購買實例從任意地域購買實例,或者繼續到下一步創建新實例。

2. 單擊側邊欄菜單中的實例。在實例列表中選擇所需地域,然後單擊右上方欄中的創建實例。

● 將其保存到應用程序目錄上的 uwsgi.ini。如需進一步了解如何編寫 ini 文件,請參閱Python/WSGI 應用程序快速入門。運行此命令以啟動您的應用程序。

uwsgi uwsgi.ini (您的 ini 文件)

12. 更改 nginx 配置文件以用於應用程序。

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name localhost; location /static/ { include uwsgi_params; alias /root/todoApp/public/; } location / { include uwsgi_params; uwsgi_pass unix:/tmp/uwsgi.sock; } }

13. 重啟 nginx,隨後您的應用程序將啟動並在埠 80 上的 nginx 之後運行。

結論

在本文中,我們討論了如何在阿里雲上部署 Django 應用程序。概括地說,第一步是(利用 Ubuntu 操作系統)運行 ECS 實例。然後,您可以在此實例上安裝和部署 Django 應用程序。請注意前提條件,確保順利完成部署。

⑽ 如何創建一個Django網站

本兄蠢文演示如何創建一個簡單的 django 網站,使用的 django 版本為1.7。

1. 創建項目
運行下面命令就可磨塵和以創建一個 django 項目,項目名稱叫 mysite :瞎盯

$ django-admin.py startproject mysite
創建後的項目目錄如下:

mysite
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

1 directory, 5 files
說明:

__init__.py :讓 Python 把該目錄當成一個開發包 (即一組模塊)所需的文件。 這是一個空文件,一般你不需要修改它。
manage.py :一種命令行工具,允許你以多種方式與該 Django 項目進行交互。 鍵入python manage.py help,看一下它能做什麼。 你應當不需要編輯這個文件;在這個目錄下生成它純是為了方便。
settings.py :該 Django 項目的設置或配置。
urls.py:Django項目的URL路由設置。目前,它是空的。
wsgi.py:WSGI web 應用伺服器的配置文件。更多細節,查看 How to deploy with WSGI
接下來,你可以修改 settings.py 文件,例如:修改 LANGUAGE_CODE、設置時區 TIME_ZONE

SITE_ID = 1

LANGUAGE_CODE = 'zh_CN'

TIME_ZONE = 'Asia/Shanghai'

USE_TZ = True
上面開啟了 [Time zone](https://docs.djangoproject.com/en/1.7/topics/i18n/timezones/) 特性,需要安裝 pytz:

$ sudo pip install pytz
2. 運行項目
在運行項目之前,我們需要創建資料庫和表結構,這里我使用的默認資料庫:

$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying sessions.0001_initial... OK
然後啟動服務:

$ python manage.py runserver
你會看到下面的輸出:

Performing system checks...

System check identified no issues (0 silenced).
January 28, 2015 - 02:08:33
Django version 1.7.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
這將會在埠8000啟動一個本地伺服器, 並且只能從你的這台電腦連接和訪問。 既然伺服器已經運行起來了,現在用網頁瀏覽器訪問 http://127.0.0.1:8000/。你應該可以看到一個令人賞心悅目的淡藍色 Django 歡迎頁面它開始工作了。

你也可以指定啟動埠:

$ python manage.py runserver 8080
以及指定 ip:

$ python manage.py runserver 0.0.0.0:8000
3. 創建 app
前面創建了一個項目並且成功運行,現在來創建一個 app,一個 app 相當於項目的一個子模塊。

在項目目錄下創建一個 app:

$ python manage.py startapp polls
如果操作成功,你會在 mysite 文件夾下看到已經多了一個叫 polls 的文件夾,目錄結構如下:

polls
├── __init__.py
├── admin.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py

1 directory, 6 files
4. 創建模型
每一個 Django Model 都繼承自 django.db.models.Model
在 Model 當中每一個屬性 attribute 都代表一個 database field
通過 Django Model API 可以執行資料庫的增刪改查, 而不需要寫一些資料庫的查詢語句
打開 polls 文件夾下的 models.py 文件。創建兩個模型:

import datetime
from django.db import models
from django.utils import timezone

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
然後在 mysite/settings.py 中修改 INSTALLED_APPS 添加 polls:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
在添加了新的 app 之後,我們需要運行下面命令告訴 Django 你的模型做了改變,需要遷移資料庫:

$ python manage.py makemigrations polls
你會看到下面的輸出日誌:

Migrations for 'polls':
0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice
你可以從 polls/migrations/0001_initial.py 查看遷移語句。

運行下面語句,你可以查看遷移的 sql 語句:

$ python manage.py sqlmigrate polls 0001
輸出結果:

BEGIN;
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
CREATE TABLE "polls_choice__new" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));
INSERT INTO "polls_choice__new" ("choice_text", "votes", "id") SELECT "choice_text", "votes", "id" FROM "polls_choice";
DROP TABLE "polls_choice";
ALTER TABLE "polls_choice__new" RENAME TO "polls_choice";
CREATE INDEX polls_choice_7aa0f6ee ON "polls_choice" ("question_id");

COMMIT;
你可以運行下面命令,來檢查資料庫是否有問題:

$ python manage.py check
再次運行下面的命令,來創建新添加的模型:

$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, polls, auth, sessions
Running migrations:
Applying polls.0001_initial... OK
總結一下,當修改一個模型時,需要做以下幾個步驟:

修改 models.py 文件
運行 python manage.py makemigrations 創建遷移語句
運行 python manage.py migrate,將模型的改變遷移到資料庫中
你可以閱讀 django-admin.py documentation,查看更多 manage.py 的用法。

創建了模型之後,我們可以通過 Django 提供的 API 來做測試。運行下面命令可以進入到 python shell 的交互模式:

$ python manage.py shell
下面是一些測試:

>>> from polls.models import Question, Choice # Import the model classes we just wrote.

# No questions are in the system yet.
>>> Question.objects.all()
[]

# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())

# Save the object into the database. You have to call save() explicitly.
>>> q.save()

# Now it has an ID. Note that this might say "1L" instead of "1", depending
# on which database you're using. That's no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
>>> q.id
1

# Access model field values via Python attributes.
>>> q.question_text
"What's new?"
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)

# Change values by changing the attributes, then calling save().
>>> q.question_text = "What's up?"
>>> q.save()

# objects.all() displays all the questions in the database.
>>> Question.objects.all()
[<Question: Question object>]
列印所有的 Question 時,輸出的結果是 [<Question: Question object>],我們可以修改模型類,使其輸出更為易懂的描述。修改模型類:

from django.db import models

class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text

class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
接下來繼續測試:

>>> from polls.models import Question, Choice

# Make sure our __str__() addition worked.
>>> Question.objects.all()
[<Question: What's up?>]

# Django provides a rich database lookup API that's entirely driven by
# keyword arguments.
>>> Question.objects.filter(id=1)
[<Question: What's up?>]
>>> Question.objects.filter(question_text__startswith='What')
[<Question: What's up?>]

# Get the question that was published this year.
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>

# Request an ID that doesn't exist, this will raise an exception.
>>> Question.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Question matching query does not exist.

# Lookup by a primary key is the most common case, so Django provides a
# shortcut for primary-key exact lookups.
# The following is identical to Question.objects.get(id=1).
>>> Question.objects.get(pk=1)
<Question: What's up?>

# Make sure our custom method worked.
>>> q = Question.objects.get(pk=1)

# Give the Question a couple of Choices. The create call constructs a new
# Choice object, does the INSERT statement, adds the choice to the set
# of available choices and returns the new Choice object. Django creates
# a set to hold the "other side" of a ForeignKey relation
# (e.g. a question's choice) which can be accessed via the API.
>>> q = Question.objects.get(pk=1)

# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
[]

# Create three choices.
>>> q.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>
>>> q.choice_set.create(choice_text='The sky', votes=0)
<Choice: The sky>
>>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)

# Choice objects have API access to their related Question objects.
>>> c.question
<Question: What's up?>

# And vice versa: Question objects get access to Choice objects.
>>> q.choice_set.all()
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
>>> q.choice_set.count()
3

# The API automatically follows relationships as far as you need.
# Use double underscores to separate relationships.
# This works as many levels deep as you want; there's no limit.
# Find all Choices for any question whose pub_date is in this year
# (reusing the 'current_year' variable we created above).
>>> Choice.objects.filter(question__pub_date__year=current_year)
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]

# Let's delete one of the choices. Use delete() for that.
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking')
>>> c.delete()
>>>
上面這部分測試,涉及到 django orm 相關的知識,詳細說明可以參考 Django中的ORM。

5. 管理 admin
Django有一個優秀的特性, 內置了Django admin後台管理界面, 方便管理者進行添加和刪除網站的內容.

新建的項目系統已經為我們設置好了後台管理功能,見 mysite/settings.py:

INSTALLED_APPS = (
'django.contrib.admin', #默認添加後台管理功能
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mysite',
)
同時也已經添加了進入後台管理的 url, 可以在 mysite/urls.py 中查看:

url(r'^admin/', include(admin.site.urls)), #可以使用設置好的url進入網站後台
接下來我們需要創建一個管理用戶來登錄 admin 後台管理界面:

$ python manage.py createsuperuser
Username (leave blank to use 'june'): admin
Email address:
Password:
Password (again):
Superuser created successfully.
總結
最後,來看項目目錄結構:

mysite
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
├── polls
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ ├── models.py
│ ├── templates
│ │ └── polls
│ │ ├── detail.html
│ │ ├── index.html
│ │ └── results.html
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
└── templates
└── admin
└── base_site.htm
通過上面的介紹,對 django 的安裝、運行以及如何創建視 圖和模型有了一個清晰的認識,接下來就可以深入的學習 django 的自動化測試、持久化、中間件、國 際 化等知識。