This is an updated approach to create Django project by Pycharm community version which is much easier and more reasonable than the former one: Django Web created by Pycharm+Command

1.Pycharm - new project

1.png

we don’t use pycharm to create a virtual environment.

2.Command

$cd PycharmProjects

$cd 180124_3

$python3 -m venv my_venv

$cd my_venv

$source bin/activate

$pip install django

$django-admin.py startproject my_project

$cd my_project

$python manage.py migrate

$python manage.py runserver

2.png

3.Run 127.0.0.1:8000

3.png

4.python manage.py startapp django_web

$python manage.py startapp django_web

5.Add django_web to installed app

my_project/settings.py

4.png

6.mkdir templates under django_web

mkdir templates under django_web copy index.html to templates folder

7.views.py

django_web/views.py

def index(request):
    return render(request, 'index.html')

5.png

8.urls.py

my_project/urls.py


from django_web.views import index
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', index),
]

6.png

9.Get access to 127.0.0.1:8000/index

7.png

it can run normally except css. then we will continue to add css to index.html

10.”static”

$mkdir static, copy ‘css’ and ‘images’folder to “static”

go to templates/index.html, put the following code on the 1st line of this html file


  {% load static %}

change all the css styles and image paths by using {% static 'oringin path' %},sample as follows


<link rel="stylesheet" type="text/css" href="{% static 'css/semantic.css' %}">
<img src="{% static 'images/0001.jpg' %}" width="100" height="91">

1.png

but, the “static files” may not be found by codes, so we need to set the absolute path in settings.py


STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

2.png

Done, html can run with css and give up a wonderful page!!

3.png

Tipping