pytharm django mysql_Pycharm+MySQL+Django开发web界面(入门练习),Pycharmmysqldjango,网页

首先,先写一个练手的页面。没有登录注册功能,纯属熟悉一下许久未用的html5代码。。嘤~

这是一个在网页中通过文本框输入或者下拉框选择用户名和密码,然后添加到界面显示的表格中去的页面

1.settings.py的templates需要修改,才能显示出templates中的html文件内容

TEMPLATES = [

{

'BACKEND': 'django.template.backends.django.DjangoTemplates',

'DIRS': [os.path.join(BASE_DIR, 'templates')],

# 修改行'DIRS': [],

'APP_DIRS': True,

templates需要与模板文件名templates一样

2.配置项目的urls文件,不是app的

这里有三种配置路径的方法,不加注释的是其中一种。

from django.contrib import admin

from django.urls import include, path

from tea1 import views

from django.conf.urls import url

urlpatterns = [

# path('', include('tea1.urls')),

# 第二种方法,将views.index路径设在同文件夹的tea1.urls中,然后在这个urls文件中设置路径到tea1.urls

path('', views.index),

# 因为有了from tea1 import views,所以可以直接调用('', views.index),就是tea1中的views.py中的index

path('admin/', admin.site.urls),

path('index.html', views.index)

#

# url(r'^index/', views.index)

]

如果用到了path('', include('tea1.urls')),

就需要配置app中的urls文件

from django.urls import path

from . import views

urlpatterns = [

path('', views.index, name='index'),

]

3.第三步。配置views文件

from django.shortcuts import render

from django.http import HttpResponse

# Create your views here.

# 先定义一个数据列表,

list = [{"name": 'lili', 'password': '0919'}, {'name': 'huahua', 'password': 'youguess'}]

def index(request):

# 获取前端post过来的用户名和密码

name = request.POST.get("name", None)

password = request.POST.get("password", None)

#把用户和密码组装成字典

data = {"name":name, "password":password}

list.append(data)

return render(request, 'index.html', {'form':list})

# 通过render模块把index.html这个文件返回到前端,并且返回给了前端一个变量form,在写html时可以调用这个form来展示list里的内容

# 最开始的一行代码 return HttpResponse("Hello.world!You're at the tea1 index!")

# render(request, 'archive.html')

4.第四步,在templates文件夹中新建index.html,写如下内容

Test

{% csrf_token %}

用户名:

密码:

用户名密码

{% for line in form %}

{{line.name}}{{line.password}}

{% endfor %}

5.终端输入python manage.py runserver运行

6c0c68c4eeb0000d8e1e14ff4342f2e3.png

6.界面如下

bd7d6bb6bed51e08442c1c83d22f2b34.png

aaa293f4b9ee56b248eaf68d601e8452.png

e4b1a82429f75554b5e2d1113bf24d73.png

3bc3dd5a45893177823ffe9f2091d855.png

d684a93898184b08780901d0a54a2045.png

下面。配置static路径

1.创建static文件夹,目录如下图:

fe6155270588f935cf346acc73ff9486.png

注意static文件与template同级

2.设置settings.py文件

文件末尾追加:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static').replace('\\', '/')

STATICFILES_DIRS = (

('css', os.path.join(STATIC_ROOT, 'css').replace('\\', '/')),

('images', os.path.join(STATIC_ROOT, 'images').replace('\\', '/')),

('font', os.path.join(STATIC_ROOT, 'font').replace('\\', '/')),

)

文件中间处添加:

TEMPLATES = [

{

'BACKEND': 'django.template.backends.django.DjangoTemplates',

'DIRS': [os.path.join(BASE_DIR, 'templates/'),

os.path.join(BASE_DIR, 'static').replace('\\', '/')# 添加行

],

3.配置urls.py文件

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

文件末尾添加:

urlpatterns += staticfiles_urlpatterns()

4.然后在html中添加想要插入的静态文件路径就行啦

比如插入一张图的格式,记得static前要加/。

11.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值