如何创建Django一个项目?helloworld 详细指南 (包括常见问题及解决方式)

这次的文章是关于django 磨人的小妖精
本文的基本内容是,如何创建一个django项目,以及在创建过程中我遇到的问题与解决方式
附上:django官方文档:https://docs.djangoproject.com/zh-hans/3.0/
官方文档可选中文!!!不要担心的点进去吧!!!
在这里插入图片描述

开始啦

  • 安装django(不赘述,此处提供pip的安装方法,更详细的过程请看文档)
    在这里插入图片描述

  • 创建第一个项目
    首先在命令行中用python -m django --version查看是否安装成功,获取到版本号
    检查没问题之后,cd到放置代码的目录,用django-admin startproject mysit来创建一个mysite目录,里面包含着的django的框架

  • 简易服务器
    切换到mysite目录中,输入
    python manage.py runserver

正确的输出是:
Performing system checks…

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run ‘python manage.py migrate’ to apply them.

八月 01, 2018 - 15:50:53
Django version 2.0, using settings ‘mysite.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

问题一:
但是如果你看到的输出是:

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.  
Run 'python manage.py migrate' to apply them. May 08, 2016 - 17:47:21 Django version 1.9.6, using settings 'mysite.settings'                        
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CTRL-BREAK. 

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044E3488>
Traceback (most recent call last):   
File "C:\Python34\lib\site-packages\django-1.9.6py3.4.egg\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)   File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\core\management\commands\runserver.py", line 137, in  inner_run
    ipv6=self.use_ipv6, threading=threading)   File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\core\servers\basehttp.py", line 188, in run
    httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)   File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\core\servers\basehttp.py", line 73, in __init__
    super(WSGIServer, self).__init__(*args, **kwargs)   File "C:\Python34\lib\socketserver.py", line 430, in __init__
    self.server_bind()   File "C:\Python34\lib\site-packages\django-1.9.6-py3.4.egg\django\core\servers\basehttp.py", line 77, in server_bind
    super(WSGIServer, self).server_bind()   File "C:\Python34\lib\wsgiref\simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)   File "C:\Python34\lib\http\server.py", line 135, in server_bind
    self.server_name = socket.getfqdn(host)   File "C:\Python34\lib\socket.py", line 463, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8e in position 0: invalid start byte

原因 :
你的PC机的名字非ASCII或者说,有中文。。改成英文就好了

问题二:
如果你发现自己改了名字之后发现自己的网页not found 在命令行中显示cant get balabala,
解决方法
试一下python manage.py migrate,就ok了。
看到这个小火箭就表示你的服务器启动成功了

  • 创建自己的项目
    依旧在mysite目录中,我们输入

python manage.py startapp your_app_name

就会创建一个你的应用的目录,假设你的应用叫:MyAPP
就会得到一个
MyAPP/
init.py
admin.py
apps.py
migrations/
init.py
models.py
tests.py
views.py
这样的目录结构

  • 编写视图(从hello world开始)

在我们的MyAPP/views.app输入
from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. ")
这就是一个最简单的helloworld视图,做了这一步之后需要用一个URL映射,所以我们需要在MyAPP的目录中新建MyAPP.py文件,输入如下代码

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

现在我们需要在跟URLconf文件中include我们的MyAPP.urls模块进去,改动如下

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

最后再cmd中再输入
python manage.py runserver
打开这个网址:http://localhost:8000/MyAPP/
在这里插入图片描述
出现helloworld你就成功了:)

  • 数据库配置
    在这里我们就使用了SQLite作为默认的数据库了
    首先,我们还是用:

    python manage.py migrate
    来创建数据表,它检查的是setting中的installed_apps设置,为其中的每个应用都创建需要的数据表。

  • 创建模型
    官方文档中创建的是投票应用,所以它需要创建两个模型,问题 Question 和选项 Choice。Question 模型包括问题描述和发布时间。Choice 模型有两个字段,选项描述和当前得票数。每个选项属于一个问题。

需要在polls/models.app文件中加上:

from django.db import models

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


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

问题3:在模型中删除了一个变量之后仍然出现之前的问题
A3:找到项目应用文件夹,删除migration文件夹,再重新之前的操作,也就是这三位
运行python manage.py makemigrations myapp 修改部分迁移
运行 python manage.py makemigrations 为模型的改变生成迁移文件。
运行 python manage.py migrate 来应用数据库迁移。

问题4:在已经建立了数据的表中增添变量(列),出现table XXX no column XXX 的错误
A4:为新增添的列增加default=‘’。

问题5: 出现class ‘’ has no ‘objects’ member
在这里插入图片描述
A5: 在model.py的class中(比如我这个就是class Font)添加

objects = models.Manager()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值