PyCharm创建django项目
前提条件:
需要安装django,可以参考如下连接:
https://www.cnblogs.com/hxlasky/p/18270726
1.创建项目
发现报错误:
解决办法:
https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/
2.查看版本
python -m django --version
3.启动项目
python manage.py runserver
解决办法:
找到 seetings.py 文件,然后在settings.py文件头加上:import os
继续启动
python manage.py runserver
4.修改settings.py
修改结果如下:
ALLOWED_HOSTS = ['*']
DEBUG = False
5.解决告警问题
Ctrl+C停掉后执行
python manage.py migrate
说明:
1.创建项目的同时会自动创建一个与项目名相同的app,该app名称是可以修改的,但是修改后需要修改配置文件settings.py的配置项:ROOT_URLCONF
2.启动的时候可以本机的ip和指定端口,如下:
python manage.py runserver 192.168.1.102:8080
或是修改manae.py文件,指定ip和端口
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from django.core.management.commands.runserver import Command as runserver
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hxlcmdb.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
runserver.default_addr = '127.0.0.1'
runserver.default_port = '9090'
main()