First Django Project.

Assume you have Django installed already.
python -m django --version


  1. Creating a project
    cd into a directory where you’d like to store your code
    django-admin startproject your_project_name

    This will create a directory(your_project_name) in your current directory
    If it didn’t work, see https://docs.djangoproject.com/en/2.1/faq/troubleshooting/#troubleshooting-django-admin

    What startproject created:

     your_project_name/
         manage.py     # It puts your project’s package on sys.path.
                       # It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
         your_project_name/
     	  __init__.py  # An empty file that tells Python that this directory should be considered a Python package.
     	  settings.py  # Contains all the configuration of your Django installation. 
     	  urls.py      # A clean, elegant URL scheme is an important detail in a high-quality Web application. informally called URLconf
     	  wsgi.py      # Django’s primary deployment platform is WSGI, the Python standard for web servers and applications.
     				   # Django’s startproject management command sets up a simple default WSGI configuration for you
     				   # more: https://docs.djangoproject.com/en/2.1/howto/deployment/wsg
    

    Verify your Django project works.
    python3 manage.py runserver 0:80
    0:80 mean 0.0.0.0:80, default was 127.0.0.1:8000

.


  1. Create your app
    An app is a Web application that does something.
    A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
    python3 manage.py startapp your_app_name

    What startapp created:

    your_app_name/
        __init__.py
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        tests.py
        views.py
    

  1. Write views that actually do something

    Create a URLconf in your app directory. Your just need create a file called urls.py.

    1. Create template file
      Create a directory called templates in your app directory.
      Create another directory called your_app_name in templates, and within that create a file called index.html

      <h1>Hello, {{name}}</h1>
      
    2. Add a view
      A view is a “type” of Web page in your Django application that generally serves a specific function and has a specific template.

      In your_app_name/views.py

      from django.http import HttpResponse
      
      def index(request):
          template = loader.get_template('travellog/index.html')
          context = {
              'name': "agnis",
          }
          return HttpResponse(template.render(context, request))
      
      

      In the your_app_name/urls.py

      from django.urls import path 
      from . import views
      
      urlpatterns = [
          path('', views.index, name='index'),
      ]
      
    3. Add your app URLconf to your_project_name/urls.py file

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

      The include() function allows referencing other URLconfs. The idea behind include() is to make it easy to plug-and-play URLs.

      path( route, view [, kwargs, name] )
      route: is a string that contains a URL pattern.
      view: a specified view function.
      kwargs: any “captured” values from the route as keyword arguments.
      name: naming your URL lets you refer to it unambiguously from elsewhere in Django.

    Run server: python3 manage.py runserver
    在这里插入图片描述
    *My app_name is “travel”.

.


  1. Database setup

    In your_project_name/setting.py file

    DATABASES
    A dictionary containing the settings for all databases to be used with Django.
    The DATABASES setting must configure a default database; any number of additional databases may also be specified.

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'db_name',
            'USER': 'db_connect_username',
            'PASSWORD': 'db_connect_password',
            'HOST': 'connect_ip',
            'PORT': '3306',
        }
    }	
    

    INSTALLED_APPS
    By default, INSTALLED_APPS contains the following apps, all of which come with Django:

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth', #  An authentication system.
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles', #  A framework for managing static files.
    ]
    

    Some of these applications make use of at least one database table,
    though, so we need to create the tables in the database before we can use them.
    python manage.py migrate
    The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app

    what migrate created:
    In your database

    auth_group
    auth_group_permissions
    auth_permission
    auth_user
    auth_user_group
    auth_user_user_permissions
    django_admin_log
    django_content_type
    django_migrations
    django_session
    

    Creating models
    Edit the your_app_name/models.py file.
    ex:

    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)
    

    That small bit of model code gives Django a lot of information. With it, Django is able to:
    Create a database schema (CREATE TABLE statements) for this app.
    Create a Python database-access API for accessing Question and Choice objects.

    Add a reference to its configuration class in the INSTALLED_APPS setting. The PollsConfig class is in the polls/apps.py file.

    INSTALLED_APPS = [
        ..... ,
        your_app_name.apps.Your_app_nameConfig,
    ]
    

    Now Django knows to include the polls app.

    python3 manage.py makemigrations your_app_name
    By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.
    Migrations are how Django stores changes to your models (and thus your database schema) - they’re just files on disk. You can read the migration for your new model if you like; it’s the file your_app_name/migrations/0001_initial.py.

    The sqlmigrate command takes migration names and returns their SQL string.
    python3 manage.py sqlmigrate polls 0001

    Run migrate again to create those model tables in your database.
    python3 manage.py migrate

.


待更新 。。。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值