pycharm问题集锦
1.如何安装python和pycharm,
百度找即可,我装的是python3.0和pycharm 2018版本
2.如何破解pycharm
https://www.cnblogs.com/zhengxu/articles/9145480.html
3.如何在pycharm平台下的Django
按照这篇文章部署
https://blog.csdn.net/Sunshine_ZCC/article/details/73918408 感谢Sunshine_ZCC的详细说明
https://www.cnblogs.com/benshan/p/4445074.html
djangoproject官方文档
https://docs.djangoproject.com/en/2.0/howto/static-files/
三个关键点
一.在pycharm下方的terminal终端中输入命令:
python manage.py startapp cmdb
这样就创建了一个叫做cmdb的APP,django自动生成“cmdb”文件夹。
python manage.py startapp cmdb
如图
二.编写路由
路由都在urls文件里,它将浏览器输入的url映射到相应的业务处理逻辑。在mysite的urls.py
简单的urls编写方法如下图(该图的代码已经修正到PYCHARM 2018最新版本):
from django.contrib import admin from django.urls import path from cmdb import views urlpatterns = [ path("index/", views.index), path('',views.index,name='index'), ]
三.编写业务处理逻辑
业务处理逻辑都在APP(本例名为cmdb)下的views.py文件里
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): return HttpResponse("My Project!")
4.如何在pycharm中启动terminal
方法一:点击左下角
QQ截图20180607152745.jpg
方法二:
ide左上角菜单 View/toolwindow/teminal
-------------------------------------------------------------------------------------
5.首次Django运行报错
WARNINGS:
?: (2_0.W001) Your URL pattern '^index/' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
解决:
从Django2.0开始,urls.py配置方法有很大改变。
1.把url函数换成path
2.不在使用^、$作为路由
3.其他地方以后再进一步研究
下面看一个列子:
from django.contrib import admin
from django.urls import path #此处应用path,老板本使用的是url
from appchat import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.acc_login,name='login'),
path('logout/', views.acc_logout, name='logout'),
path('',views.index,name='index'), #此处设置为首页,以前写法是'^$',新版本不再使用^、$,只需要‘’就可以
]
解决如图
如果使用了^、$会有一个警告,但是这个警告是严重的,页面也不能正常访问,错误如下:
WARNINGS:
?: (2_0.W001) Your URL pattern '^$/' [name='index'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
-------------------------------------------------------------------------------------
6.返回HTML文件
7.taberror inconsistent use of tabs and spaces in indentation pycharm
pycharm编写python代码时,有时从不同的文件复制代码,粘贴后会出错,
taberror inconsistent use of tabs and spaces in indentation pycharm
解决方法,选择菜单栏:
Edit -> Convert Indents -> To Spaces
8.windows下Python安装pymysql 『FROM: http://www.cnblogs.com/snn0605/p/6658009.html』
(venv) C:\Users\zheng\PycharmProjects\mysite>pip list Package Version ---------- ------- Django 2.0.6 pip 10.0.1 PyMySQL 0.8.1 pytz 2018.4 setuptools 39.1.0
下载 https://github.com/PyMySQL/PyMySQL
安装pymysql命令
从github 下载pymysql 安装包
下载地址:https://github.com/PyMySQL/PyMySQL
解压下载文件,不用打开python交互界面,cd到解压文件所在目录
运行 python setup.py install
打开python交互界面,
运行 import pymysql,如果不报错,说明安装成功!
下面是安装截图
如:我的下载文件解压位置为:C:\Users\ideayl\Downloads\PyMySQL-master\PyMySQL-master
输入 python setup.py install运行,最后会出现Finished dependencies…………
运行python,输入import pymysql
没有报错,安装成功。
9.django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
这个问题困扰了我很久,后来找到了这篇文章解决https://blog.csdn.net/shishui07/article/details/80603953
登录到mysql,并执行如下两条命令(newpassword为新密码)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'; FLUSH PRIVILEGES;
同时将setting.py中的password字段改成新设置的newpassword