Django learning (the third day: my first view) create app static resources viewsurls running results

Django learning (the third day: my first view) create app static resources viewsurls running results

Create app

For web novices, these professional names are completely daunting. Fortunately, the beautiful sister speaks very well. I think that if a website is equivalent to a project, then each small function is an app. There is no way to use the beautiful sister. Instead, use Terminal to create an app in pycharm:

python manage.py startapp investigate

Here you need to configure the newly created app in settings

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

Static resources

  • Create html file in templates
  • Create static in the root directory, create css and images files.

The files here are all provided by the beautiful sister.

views

def showform(request):
    return render(request,'form.html')

urls

from investigate import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^form/$',views.showform)
]

If you run this way, there is no css style, we need to configure it in settings. Also, my Django version is different, and an error is reported when I run: The template cannot be found, and it needs to be set in the configuration here.

# TEMPLATE_DIRS = (os.path.join(BASE_DIR,'templates'),)
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,"static")
]


TEMPLATES = [
    {
        'BACKEND':'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

operation result

Reference: https://cloud.tencent.com/developer/article/1155623 Django learning (the third day: my first view) create app static resources viewsurls running results-Cloud + Community-Tencent Cloud