導航:首頁 > 配伺服器 > 如何用本地伺服器django建站

如何用本地伺服器django建站

發布時間:2023-02-05 08:40:41

A. 怎麼用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」 的提示。

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

B. 怎樣搭建Django伺服器環境

1.首先安裝python,配置環境變數path:C:Python27;C:Python27Scripts;

2.去django官網下載壓縮包Django-1.8.3.tar.gz,然後解壓在C盤,輸入以下命令

cdC:Django-1.8.3

pythonsetup.pyinstall

命令運行後,Django環境就安裝好了,然後配置環境變數path:C:Python27Libsite-packagesDjango-1.8.3-py2.7.eggdjangoin

3.在命令終端輸入以下命令導入並檢查django安裝情況:

python

>>>importdjango

>>>django.VERSION

__init__.py:將這個項目目錄作為Python的一個包。

settings.py:項目的配置文件。

urls.py:定義了Django項目中的URL路由表,指定了URL與被調用類之間的對應關系。

wsgi.py:這個是Django1.4中新添加的默認Web伺服器網關介面。

命令窗口切換到cms678文件夾,然後運行命令:pythonmanage.pyrunserver,啟動當前目錄工程。

瀏覽器輸入http://127.0.0.1:8000/

到此基本操作就結束啦:-)

C. 用pyqt做好了前端,想用django做伺服器,想問一下大概怎麼搭建呢

一般客戶端(也就是你說的前段)跟伺服器端(你准備使用Django)都是通過 HTTP 協議交換信息的(除非有特別的需求,才會使用別的或者定製協議)。


在 客戶端(PyQT)中,你可以安裝 Requests 庫,它可以幫助你發送 HTTP 請求給伺服器端,

在 Django 中你可以使用 Django REST Framework 網頁鏈接處理 客戶端的HTTP請求。

D. 如何創建一個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 的自動化測試、持久化、中間件、國 際 化等知識。

E. 用自己的伺服器建設網站要做些什麼

你要用自己的伺服器建設網站,要分幾部分:
1、網站建設
2、伺服器建設
3、網站在伺服器部署
4、網路建設

一、網站建設
這部分指的是網站的製作。你可以自己做,也可以找公司製作。這里不做詳細介紹。
這部分完成的標志,是在Intranet上可以正常訪問

二、伺服器建設
這步包含安裝伺服器系統(系統必須安裝server版)、支持網站的伺服器(例如:asp、.net、php需要安裝iis伺服器,jsp需要安裝tomcat伺服器等)、網站使用的資料庫(例如:SQL Server、MySQL、Access等等,具體視網站的需要而定),另外根據具體的需要還可以安裝一些FTP工具、遠程訪問工具和一些殺毒軟體防火牆軟體。

三、網站在伺服器部署
這一步要視網站編程語言和資料庫而定,類似於本地部署,但根據系統的差異略有不同。網上有很多網站部署方面的文章你可以參考一下。
二、三完成的標志,是可以在伺服器上本地瀏覽網站

四、網路建設
看你又是硬防,又是交換機路由器的。應該是資金比較充裕。建議你們最好是有個專業的網路工程師給你們做網路建設和維護。因為這步的主要目的是保持網路暢通,網站正常訪問,防止病毒、木馬和黑客的攻擊。

F. 怎麼在vps上搭建django伺服器

安裝pip
下載
[plain] view plain
#wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gz
解壓並進入解壓後的文件夾
[plain] view plain
#tar zvxf 1.5.5.tar.gz
#cd pip-1.5.5/
安裝
[plain] view plain
#python setup.py install

四、使用pip安裝django
[plain] view plain
#pip install django

G. 大概會Django了,然後怎麼建自己的網站

  1. 准備一個域名

  2. 准備一個空間或者伺服器

  3. 准備一個站點程序

  4. 空間綁定、域名解析、程序存放到空間

  5. 代碼安裝

H. Django部署——uwsgi+Nginx(超詳細)

環境:
python3.6
centos 7
Django1.11
用Django寫了個小網站,只能在自己本地跑一跑!這怎麼行?聽說可以部署在雲伺服器上,這樣別人就可以訪問了!

從哪兒開始?就從Django開始吧!老規矩,按步驟:

這里不講Django項目實施過程,假設你已經寫了一個Django項目,並且在本地 127.0.0.1:8000 能夠跑起來。喏,給你個參考,項目大概長這樣:

也就是項目目錄下的settings.py文件,主要強調幾個地方:
①關閉DEBUG模式:

②修改ALLOWED_HOSTS:

③配置靜態文件存放路徑:

修改好配置之後執行:

這個沒什麼說的。。。在自己的雲伺服器上裝好這兩個工具
安裝好uwsgi後最好驗證一下,驗證方法:
創建一個test.py文件:

啟動uwsgi伺服器:

如果可以正常啟動而不報錯那就應該沒問題,不放心的話再在終端驗證一下:

在uwsgi.ini里進行如下配置:

找到nginx的配置文件夾,centos7的nginx配置文件在/etc/nginx下,該路徑下有一個nginx.conf總配置文件,還有兩個文件夾./conf.d、./default.d,我們將nginx.conf復制一份到conf.d文件夾下,命名為nginx.conf(或者項目名.conf)進行如下修改(根據中文注釋進行相應配置即可):

進入uwsgi.ini文件夾下執行:

在終端執行:

參考資料:
劉江的博客
博客園
知乎問答
無名Blog
自強學堂Django教程
Django文檔
empty_xl Blog

I. 怎樣搭建Django伺服器環境

搭建Django伺服器環境方法詳見:http://jingyan..com/article/8ebacdf029d43549f75cd548.html

J. 如何在伺服器上部署Django項目並使其在後台一直運行

前幾天老師讓我把一個Django項目(爬蟲網頁)放到校園內網上,但是我想先用自己的伺服器來嘗試一下。之前剛好有在Digital Ocean上買過伺服器用來運行ss腳本,平時伺服器一直放著沒啥用,所以就拿它來試驗一下。

廢話不多說,第一步通過WinSCP軟體把Django文件傳到伺服器上。

在伺服器中安裝Django需要的環境和我所需要的Python第三方庫。

以上所有步驟完成後,還需要進行一步操作,這是我經歷的一個 。 打開Django文件目錄中的 settings.py ,把 ALLOWED_HOSTS=[] 改為 ALLOWED_HOSTS=["*"] 。

在伺服器中打開到 manage.py 所在的目錄,輸入命令:
python3 manage.py runserver 0.0.0.0:8000
然後按下回車,在瀏覽器中輸入: 該伺服器IP地址:8000 ,大功告成!

Attention:
1. python3 不是特定的,是根據你的Django項目所需要的環境指定的。
2. 8000 是埠號,可以修改。

如果想要Django項目一直運行,關閉終端後還在運行,即需要運行如下命令, nohup command & , command 即位上文所說的 python3 manage.py runserver 0.0.0.0:8000 。

閱讀全文

與如何用本地伺服器django建站相關的資料

熱點內容
亞馬遜店鋪可以遷移到雲伺服器嗎 瀏覽:840
真空泵壓縮比會改變嗎 瀏覽:329
示波器app怎麼看 瀏覽:612
米家app英文怎麼改 瀏覽:605
學習編程你有什麼夢想 瀏覽:886
農行信用報告解壓密碼 瀏覽:217
小程序員調試信息 瀏覽:183
電腦打代碼自帶編譯嗎 瀏覽:273
和平怎麼在和平營地轉安卓 瀏覽:463
我的世界中如何查看伺服器的人數 瀏覽:618
台式機改為網路伺服器有什麼好處 瀏覽:960
騰訊雲輕量應用伺服器如何登陸 瀏覽:620
考研復試c語言編譯器 瀏覽:150
安卓的字體怎麼變粗 瀏覽:253
java錯誤無法載入主類 瀏覽:348
程序員考試考什麼文憑 瀏覽:883
pdf版破解 瀏覽:522
安卓系統如何重啟 瀏覽:174
小天才app鬧鍾怎麼改 瀏覽:962
司馬彥PDF 瀏覽:885