⑴ 怎么将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 的自动化测试、持久化、中间件、国 际 化等知识。