django 面向切面编程_面向Django的WebApp初学者

django 面向切面编程

In this article we gonna learn how to link web application with Django framework and will gonna be using ( Intellij Pycharm ) for building our backend ( Django ). We in the first should get our website ready to link it with our backend ( Django ).

在本文中,我们将学习如何将Web应用程序与Django框架链接,并将使用(Intellij Pycharm)构建后端(Django)。 首先,我们应该准备好将网站链接到我们的后端(Django)。

After getting things up and running ( our website ) we gonna install Django by opening a CMD ( or Terminal if using Linux) and you should have installed pip ( the package installer for Python ) type the following command:

安装并运行完(我们的网站)后,我们将通过打开CMD(如果使用Linux则为Terminal)来安装Django,并且您应该已经安装了pip(Python的软件包安装程序),键入以下命令:

pip install django

After installing Django we now gonna create our Django project ( make sure to point to the location that you want it to be ) and type the following command:

安装Django之后,我们现在将创建我们的Django项目(确保指向您想要的位置),然后键入以下命令:

django-admin startproject mycvsite

Now we have our project created successfully, know we gonna make some changes to our setting.py file in ( DATABASES ) section, first step we gonna add our MySQL configuration with the following script( make sure to put your own information ):

现在我们已经成功创建了我们的项目,知道我们将在(DATABASES)部分中对setting.py文件进行一些更改,第一步,我们将使用以下脚本添加MySQL配置(确保输入您自己的信息):

DATABASES = {
‘default’:{
‘ENGINE’:’django.db.backends.mysql’,
‘NAME’:’here put your db name’,
‘USER’:’your username’,
‘PASSWORD’:’your password’,
'HOST’:’the host (localhost for now)’,
‘PORT’:’if exist put it here’,
}

Then to test if everything is correct we gonna migrate with the database by typing this command:

然后要测试一切是否正确,我们将通过键入以下命令来与数据库一起迁移:

python manage.py makemigrations
python manage.py migrate

Next step create static folder ( were our CSS/SCSS/JS )

下一步创建静态文件夹(分别是我们CSS / SCSS / JS)

mkdir static

then, we gonna add STATIC and STATICFILES file path so we put the following script in the end of the file:

然后,我们将添加STATIC和STATICFILES文件路径,因此将以下脚本放在文件末尾:

STATIC_URL = '/static/' 
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

Next we create app by typing the following script:

接下来,我们通过键入以下脚本来创建应用程序:

python3 manage.py startapp base

Add the app name to the setting.py file in the ( INSTALLED_APPS ) section by typing this script:

通过输入以下脚本,将应用程序名称添加到(INSTALLED_APPS)部分中的setting.py文件:

INSTALLED_APPS = [    'base',     
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

Now we need create two files inside each other first file is ( templates ) the second file is ( base ) and inside it we gonna add our Web App pages by typing the following command:

现在我们需要在彼此之间创建两个文件,第一个文件是(template)第二个文件是(base),然后在其中通过键入以下命令添加Web App页面:

mkdir templates  
cd templates
mkdir base

We gonna now add some functions in the views.py file that renders our templates, view it holds functions that takes requests and anything from ( http response ) to ( template ). Now we gonna add the following script:

现在,我们将在呈现我们的模板的views.py文件中添加一些函数,并查看其中包含接受请求以及从(http response)到(template)的所有内容的函数。 现在,我们将添加以下脚本:

from django.shortcuts import render  def home(request):     
return render(request, 'base/index.html')

Now we create file with the name of urls.py for our URLs routing system and insert the following command and script:

现在,我们为URL路由系统创建名称为urls.py的文件,并插入以下命令和脚本:

nano urls.py  from django.urls import path from . 
import views urlpatterns = [
path('', views.home)
]

And adding now our base app URLs to the main urls.py which gonna handle requests comes and decides where it should goes in our homepage add the following script to the file:

现在,将我们的基本应用程序URL添加到将处理请求的主urls.py中 ,并确定应在我们的主页中添加以下脚本到文件中:

from django.contrib import admin 
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('base.urls')),
]

Before we start the last step we should make sure that our pages have in the root ( in the top of the html page ) the following script to make sure the page understand where files is stored in:

在开始最后一步之前,我们应该确保我们的页面在根目录(位于html页面的顶部)中具有以下脚本,以确保该页面了解文件存储在何处:

{% load static %}

And make sure to add it in every place that you called your files in, like the following script:

并确保将其添加到调用文件的每个位置,例如以下脚本:

<li>  
<a href="https://github.com/iamabdulrazak">
<img class="social" src="{% static 'images/github.png' %}"> GitHub </a>
</li>

Know we create superuser for our admin panel and will be ask you some questions, by typing the following command:

知道我们为管理面板创建了超级用户,并且会通过键入以下命令来询问您一些问题:

python3 manage.py createsuperuser

We are now have every thing set in the right way, now we migrate all the changes to the database so we can make sure everything in the right place.

现在,我们已经以正确的方式设置了所有内容,现在我们将所有更改都迁移到数据库中,因此我们可以确保所有内容都位于正确的位置。

Hint: Django builds for us by default admin panel and setting for us also WSGI file.

提示:默认情况下,Django为我们构建管理面板,并为我们设置WSGI文件。

python3 manage.py migrate

Finally, now everything is good and in the right place so now we gonna check the application if it’s working by running the server with the following command ( check in the http://127.0.0.1:8000/ ):

最后,现在一切都很好,并且在正确的位置,所以现在我们将通过使用以下命令运行服务器来检查应用程序是否正常运行(请检查http://127.0.0.1:8000/ ):

python3 manage.py runserver

Thanks for your time to read my article and you can find my github for this project @ this link https://github.com/iamabdulrazak/webapp-django

感谢您抽出宝贵的时间阅读我的文章,并且可以在此链接的https://github.com/iamabdulrazak/webapp-django上找到该项目的github。

翻译自: https://medium.com/@zaak0/webapp-with-django-backend-for-beginners-7a6abebb108c

django 面向切面编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值