干货 | Python 实现新闻系统内容的增删改查功能

2018年5月30日10:29:15 2 774 阅读

1.选好项目所在目录,我选择在E盘,创建项目文件名news

代码:django-admin  startproject  news

干货 | Python 实现新闻系统内容的增删改查功能

 

2.利用编辑器打开文件,此图创建成功的项目

干货 | Python 实现新闻系统内容的增删改查功能

3.创建app,app名为myapp 注意要进入news文件夹,才可以创建myapp

 代码:cd news

       Python manage.py  startapp  myapp

干货 | Python 实现新闻系统内容的增删改查功能

4.创建成功的myapp

干货 | Python 实现新闻系统内容的增删改查功能

 

5.添加主路由news ->urls.py做如下添加

 


1
2
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">from</span> django.conf.urls <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> url,include
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">from</span> django.contrib <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^myapp/', include('myapp.urls')),
]


1
 

 

干货 | Python 实现新闻系统内容的增删改查功能

 

6.子路由设置:将news下的urls.py 复制一份到myapp 下,并将内容改为如图

 

 


1
2
3
4
5
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">from</span> django.conf.urls <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> url
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">from</span> . <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> views
urlpatterns = [
    url(<span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">r'^$'</span>, views.index, name=<span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 46px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'index'</span>),
]

 

干货 | Python 实现新闻系统内容的增删改查功能

 

干货 | Python 实现新闻系统内容的增删改查功能

 

7.我们来写第一个视图。打开文件myapp/views.py 并写入以下Python代码:

 


1
2
3
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">from</span> django.http <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> HttpResponse
<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 125px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">def</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">index</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 60px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(request)</span>:</span>
    <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">return</span> HttpResponse(<span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 277px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">"Hello, world. You're at the myapp index."</span>)

 

干货 | Python 实现新闻系统内容的增删改查功能

 

8.在终端运行python  manage.py  runserver 注意我所在的terminal 与cmd 进入的终端是相同的

 

干货 | Python 实现新闻系统内容的增删改查功能

 

9.网页输入localhost:8000/myapp若你出现此图说明此步骤成功啦~

 

干货 | Python 实现新闻系统内容的增删改查功能

 

10.连接MySQL设置

 

news/settings.py文件中,通过DATABASES项进行数据库设置

 


1
2
3
4
5
6
7
8
9
10
DATABASES = {
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 60px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'default'</span>: {
        <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 52px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'ENGINE'</span>: <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 171px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.db.backends.mysql'</span>,
        <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'NAME'</span>: <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 53px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'mynews'</span>,
        <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'USER'</span>: <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'root'</span>,
        <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 66px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'PASSWORD'</span>: <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 53px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'123456'</span>,
        <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'HOST'</span>: <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 73px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'localhost'</span>,
        <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'PORT'</span>: <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'3306'</span>,
    }
}

 

创建数据库mynews

 

 

干货 | Python 实现新闻系统内容的增删改查功能

 

干货 | Python 实现新闻系统内容的增删改查功能

注意:Django使用MySQL数据库需要安装PyMySQL,若已经安装请略过。

 


1
<span class="hljs-default-meta" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 6px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">$</span><span class="bash"> pip install pymysql</span>

 

打开 mynews/__init__.py ,写入以下代码导入pymysql

 


1
2
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> pymysql
pymysql.install_as_MySQLdb()

 

干货 | Python 实现新闻系统内容的增删改查功能

 

编辑 mynews/models.py文件

 


1
2
3
4
5
6
7
8
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">from</span> django.db <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">import</span> models
<span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 171px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;"># Create your models here.</span>
<span class="hljs-default-class" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 165px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">class</span> <span class="hljs-default-title" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 27px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">News</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 92px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(models.Model)</span>:</span>
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 189px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'''自定义Stu表对应的Model类'''</span>
    <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 212px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">#定义属性:默认主键自增id字段可不写</span>
    id = models.AutoField(primary_key=<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">True</span>)
    title = models.CharField(max_length=<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">255</span>)
    content = models.CharField(max_length=<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">255</span>)

# 定义默认输出格式
def __str__(self):
return        "%d:%s:%d:%s:%s"%(self.id,self.title,self.content)
# 自定义对应的表名,默认表名:myapp_stu
class Meta:
db_table="news"


1
 

 

干货 | Python 实现新闻系统内容的增删改查功能

 

要将该应用程序包括在我们的项目中,我们需要在设置中添加对其配置类的引用INSTALLED_APPS。

该 myappConfig班是在myapp/apps.py文件中,所以它的虚线路径'myapp.apps.myappConfig'。

编辑news/settings.py文件,并将该虚线路径添加到该INSTALLED_APPS设置。

 


1
2
3
4
5
6
7
8
9
<span class="hljs-default-attribute" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 92px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">INSTALLED_APPS</span>  =  [
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 145px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.contrib.admin'</span> ,
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 139px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.contrib.auth'</span> ,
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 192px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.contrib.contenttypes'</span> ,
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 165px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.contrib.sessions'</span> ,
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 165px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.contrib.messages'</span> ,
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 185px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'django.contrib.staticfiles'</span> ,
    <span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 159px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">'myapp.apps.MyappConfig'</span>,  <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 106px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">#或者直接写 myapp</span>
]

 

干货 | Python 实现新闻系统内容的增删改查功能

 

迁移数据库

 

干货 | Python 实现新闻系统内容的增删改查功能

 

添加数据

- The End -

本篇文章来源于微信公众号: 程序人生

张晔璇

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:2   其中:访客  1   博主  1

    • avatar 今天头条 0
      Google Chrome 55.0.2883.87 Google Chrome 55.0.2883.87 Windows 7 Windows 7
      Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

      文章不错非常喜欢

        • avatar 张晔璇 Admin
          QQbrowser 10.0.1313.400 QQbrowser 10.0.1313.400 Windows 7 x64 Edition Windows 7 x64 Edition
          Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5383.400 QQBrowser/10.0.1313.400

          你的网站,403

          @今天头条