django crud_Django Crud应用程序PostgreSQL

django crud

In this blog, let’s see what is CRUD and how to perform CRUD with Django. Also, visit my previous blogs if you have any problem with connecting Django and Databases. In this blog, I am performing CRUD functionality with PostgreSQL.

在此博客中,让我们看看什么是CRUD以及如何使用Django执行CRUD。 另外,如果您在连接Django和数据库方面有任何问题,请访问我以前的博客 。 在此博客中,我将使用PostgreSQL执行CRUD功能。

What is CRUD?

什么是CRUD?

CRUD is Create, Read, Update, and Delete.

CRUD是C reate,R EAD, 更新,d elete。

  1. Creating a Django Project and Database Initialization.

    创建Django项目和数据库初始化。

$ django-admin startproject curddjango
$ cd curddjango/
$ python3 manage.py runserver
Watching for file changes with StatReloaderPerforming system checks...System check identified no issues (0 silenced).You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.Run 'python manage.py migrate' to apply them.August 21, 2020 - 07:14:32Django version 3.1, using settings 'curddjango.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.

After creating a project successfully, connect to any database of your wish. Here I am connecting with PostgreSQL. Initially, to connect with PostgreSQL, we should have an adapter. Install it with the command.

成功创建项目后,连接到您想要的任何数据库。 在这里,我要连接PostgreSQL。 最初, 要连接PostgreSQL,我们应该有一个adapter 。 使用命令安装。

$ pip install postgres

After successful installation, open curddjango/settings.py. Scroll to the database section to configure our database.

成功安装后,打开curddjango/settings.py 。 滚动到数据库部分以配置我们的数据库。

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databasesDATABASES = {
'default':
'ENGINE': 'django.db.backends.postgresql'
'NAME': 'django-curd' # Databaes Name
'USER': 'postgres' # User Name
'PASSWORD': 'admin' #Password
'HOST': '127.0.0.1'
'PORT': '5432'
}
}

Now create an app and register it in the settings.py file.

现在创建一个应用程序并将其注册在settings.py文件中。

$ python3 manage.py startapp curd

The above command creates an app named curd in our project folder.

上面的命令在我们的项目文件夹中创建一个名为curd的应用程序。

Image for post

After creating, register the app in the settings.py file.

创建后,将应用程序注册到settings.py文件中。

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', 'curd', #Add your App name here]

Now just migrate your project.

现在,只需迁移您的项目即可。

$ python3 manage.py migrateOperations to perform:Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:Applying contenttypes.0001_initial... OKApplying auth.0001_initial... OKApplying admin.0001_initial... OKApplying admin.0002_logentry_remove_auto_add... OKApplying admin.0003_logentry_add_action_flag_choices... OKApplying contenttypes.0002_remove_content_type_name... OKApplying auth.0002_alter_permission_name_max_length... OKApplying auth.0003_alter_user_email_max_length... OKApplying auth.0004_alter_user_username_opts... OKApplying auth.0005_alter_user_last_login_null... OKApplying auth.0006_require_contenttypes_0002... OKApplying auth.0007_alter_validators_add_error_messages... OKApplying auth.0008_alter_user_username_max_length... OKApplying auth.0009_alter_user_last_name_max_length... OKApplying auth.0010_alter_group_name_max_length... OKApplying auth.0011_update_proxy_permissions... OKApplying auth.0012_alter_user_first_name_max_length... OKApplying sessions.0001_initial... OK

2. Creating a model and migration:

2. 创建模型并进行迁移:

Now create a new table named curd in the database. Open curd/models.py file to create our first model.

现在,在数据库中创建一个名为curd的新表。 打开curd/models.py文件创建我们的第一个模型。

from django.db import models# Create your models here.
class Emp(models.Model):
emp_name = models.TextField()
emp_email = models.EmailField()
emp_mobile = models.TextField()

Now migrate your app.

现在迁移您的应用程序。

$ python3 manage.py makemigrationsMigrations for 'curd':curd/migrations/0001_initial.py- Create model Emp$ python3 manage.py migrateOperations to perform:Apply all migrations: admin, auth, contenttypes, curd, sessions
Running migrations:Applying curd.0001_initial... OK

3. Create Template and View

3. C reate 模板和视图

Create a folder templates in the main directory where we’ll be creating our views.

在将要创建视图的主目录中创建一个文件夹templates

Now open curd/settings.py file and scroll down to the Templates section and os.path.join(BASE_DIR,’templates’) in the DIRS. This will tell Django where our templates( UI) reside.

现在打开curd/settings.py文件,向下滚动到模板部分和os.path.join(BASE_DIR,'templates')DIRS 。 这将告

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值