导航:首页 > 配服务器 > 如何用本地服务器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建站相关的资料

热点内容
罪孽泰国版电影在线观看完整版 浏览:193
小说黄色下载 浏览:579
骑手数算法 浏览:969
安卓的前端是用什么语言 浏览:950
主角叫江南的玄幻小说 浏览:493
加密人员是干什么的 浏览:572
如何开通手机imap服务器 浏览:507
博途v151软件编译好后如何仿真 浏览:429
365还有哪几种算法 浏览:737
加密数字货币和法定货币的区别 浏览:641
加密的视频如何录屏 浏览:28
java代码在eclipse哪个文件夹 浏览:222
旧的安卓线叫什么 浏览:859
台湾红羊公司出品的电影 浏览:102
红颜玫瑰花双女主免费阅读 浏览:238
小说傻柱原着txt 浏览:967
周香允演的女上市是哪部电影 浏览:423
单片机异步通信数据格式 浏览:13
argon2d算法的币 浏览:50
世界上最简单的解压神器 浏览:566