小编Mis*_*ing的帖子

Django-安装mysqlclient错误:需要mysqlclient 1.3.13或更高版本;你有0.9.3

我已经搜寻了论坛,但是找不到答案,甚至找不到任何文档。

运行命令时:

python manage.py inspectdb
Run Code Online (Sandbox Code Playgroud)

我收到错误:

mysqlclient 1.3.13 or newer is required; you have 0.9.3
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有建议的修复程序,包括:-升级pip-安装不同的滚轮(32位而不是64位),即使用命令pip install mysqlclient-1.4.2- mysqlclient-1.4.2-cp37-cp37m-win32.whl。 cp37-cp37m-win32.whl(这可以正常工作,没有错误,但不执行所需的工作!)

我的目标是简单地将旧版mysql数据库(在XAMPP和myphpadmin中运行)连接到Django。我遵循的文档遗漏了安装mysqlclient的需要,并且在这一点上遇到了麻烦。

mysql django version

18
推荐指数
5
解决办法
2万
查看次数

Django 项目:命名空间“admin”不是唯一的

在尝试运行 C:\Python34/python manage.py makemigrations 时,我收到以下错误:

错误

WARNINGS: ?: (urls.w005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLS in this namespace
Run Code Online (Sandbox Code Playgroud)

我到底需要改变什么,我需要在哪里看?

教师/url.py

from django.contrib import admin
from django.urls import path
from django.urls import include, path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.teachers, name='teachers'),

]
Run Code Online (Sandbox Code Playgroud)

网址.py

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main.urls')),
    path('teachers/', include('teachers.urls')),
]
Run Code Online (Sandbox Code Playgroud)

主/ url.py

urlpatterns = [
    path('admin/', …
Run Code Online (Sandbox Code Playgroud)

migration django admin unique models

7
推荐指数
2
解决办法
1万
查看次数

谁能解释这个索引的根超出范围错误?

以下代码引发了一个我无法找到解决方案的神秘错误.当我在一个更大的模块中测试它时它工作正常,所以无法理解为什么这不起作用:

import csv

with open('studentinfo.txt','a') as fo: #open the file in append mode (add to file, we don't wish to overwrite!)
        studentfileWriter=csv.writer(fo) #fo = file out (this can be called anything you like)
        id=input("Enter Student Id:")
        firstname=input("Enter firstname:")
        surname=input("Enter Surname:")
        test1=input("Enter test1 score:")
        test2=input("Enter test2 score:")
        test3=input("Enter test3 score:")
        studentfileWriter.writerow([id,firstname,surname,"Test1:"+test1,"Test2:"+test2,"Test3:"+test3])
        print("Record has been written to file")


with open("studentinfo.txt", "r") as f:
    reader = csv.reader(f)
    sorted_list = list(reader)  # turn the reader iterator into a list
    sorted_list.sort(key=lambda x: x[2])  # …
Run Code Online (Sandbox Code Playgroud)

python list range

5
推荐指数
1
解决办法
153
查看次数

“venv\Scripts\activate”不被识别为内部或外部命令、可操作程序或批处理文件

有一个类似的问题,但它没有处理我的错误:

在尝试在 E 驱动器中使用 CMD 安装虚拟环境时,我使用了以下命令:

E:\myproject>venv\Scripts\activate
Run Code Online (Sandbox Code Playgroud)

发生的错误是:

'venv\Scripts\activate' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

安装 Flask 所需的具体步骤是什么 - 一步一步按照文档进行操作,但不起作用。

你可以追踪我的脚步,而我却一直被困在这里。我究竟做错了什么?

E:\>mkdir myproject

E:\>cd myproject

E:\myproject>python3 -m venv venv

E:\myproject>py -3 -m venv venv
Error: Command '['E:\\myproject\\venv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 3221226505.

E:\myproject>venv\Scripts\activate
'venv\Scripts\activate' is not recognized as an internal or external command,
operable program or batch file.

E:\myproject>
Run Code Online (Sandbox Code Playgroud)

注意:您会看到我已按照此处创建虚拟环境的步骤进行操作:https://flask.palletsprojects.com/en/1.1.x/installation/#virtual-environments

我也尝试了以下方法,但也出现错误:

E:\myproject>py -3 -m venv venv
Error: …
Run Code Online (Sandbox Code Playgroud)

python virtual cmd flask

5
推荐指数
1
解决办法
2万
查看次数

创建一个 2d numpy 数组来保存字符

我有以下 numpy 数组和之前的设置,其中有一个字队列和一个临时变量“temp”来存储单词。这个单词需要一个字母一个字母地“放入”numpy 二维数组中:

from collections import deque
import numpy as np 
message=input("Write a message:")
wordqueue=message.split()
queue=deque(wordqueue)
print(wordqueue)

for i in range(1):
  temp=wordqueue.pop(0) #store the removed item in the temporary variable 'temp'
print(wordqueue)
print(temp)
display = np.zeros((4,10)) #create a 2d array that is to store the words from the queue
print(display)
display[0, 0] = temp #add the word from the temp variable to fill the array (each character in each sequential position in the array)
print(display)
Run Code Online (Sandbox Code Playgroud)

不幸的是,输出如下:

Write a message: …
Run Code Online (Sandbox Code Playgroud)

python numpy

3
推荐指数
1
解决办法
1万
查看次数

从文件中读取时剥离空格和新行

我有以下代码,在从文件读取时成功地删除行尾字符,但是对于任何前导和尾随空格都没有这样做(我希望中间的空格被留下!)

实现这一目标的最佳方法是什么?(注意,这是一个具体的例子,因此不能删除剥离字符串的一般方法)

我的代码 :(尝试使用测试数据:" Moose先生 "(未找到),如果你尝试"Moose先生"(这是Moose之后的空间),它将起作用.

#A COMMON ERROR is leaving in blank spaces and then finding you cannot work with the data in the way you want!

"""Try the following program with the input: Mr Moose
...it doesn't work..........
but if you try "Mr Moose " (that is a space after Moose..."), it will work!
So how to remove both new lines AND leading and trailing spaces when reading from a file into a list. …
Run Code Online (Sandbox Code Playgroud)

python split file

3
推荐指数
1
解决办法
3967
查看次数

在python中将文本文件内容转换为字典的最有效方法

以下代码基本上执行以下操作:

  1. 获取文件内容并将其读入两个列表(剥离和拆分)
  2. 将两个列表一起压缩成字典
  3. 使用字典创建"登录"功能.

我的问题是:是否有更简单,更有效(更快)的方法从文件内容创建字典:

文件:

user1,pass1
user2,pass2
Run Code Online (Sandbox Code Playgroud)

def login():
    print("====Login====")

    usernames = []
    passwords = []
    with open("userinfo.txt", "r") as f:
        for line in f:
            fields = line.strip().split(",")
            usernames.append(fields[0])  # read all the usernames into list usernames
            passwords.append(fields[1])  # read all the passwords into passwords list

            # Use a zip command to zip together the usernames and passwords to create a dict
    userinfo = zip(usernames, passwords)  # this is a variable that contains the dictionary in the 2-tuple …
Run Code Online (Sandbox Code Playgroud)

python dictionary file

3
推荐指数
1
解决办法
444
查看次数

在 JavaScript 脚本标签内使用 jinja2(带有 Flask)来渲染数据

正如问题所述,我只是想将列表的结果(从 Flask 项目中的 main.py 文件传递​​)传递并渲染到 html 模板(charts.html)。问题是我试图将 jinja 变量或使用 jinja 放在脚本标签(JavaScript)而不是 html 中。我找不到任何文档或答案来告诉我如何执行此操作:

我的代码:charts.html(烧瓶中的模板)

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
    <script src="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>

    <title>Webscraping and Charts</title>
</head>
<body>
    <h1>Webscraping and Beautiful Charts</h1>
<p>{{enrolled}}</p>
<div class="ct-chart .ct-perfect-fifth"></div>

<script>
var data = {
  labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
  series: [
    [15, 222, 4, 2, 0]
  ]
};
new Chartist.Line('.ct-chart', data); //this line creates the chart
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在 h1 标签下,您会注意到:

{{已注册}}

该变量的内容在 python 文件中定义。功能如下所示。已注册=[3,200,600,1800,22000]

@app.route('/charts')
def charts():
    enrolled=[3,200,600,1800,22000]
    return …
Run Code Online (Sandbox Code Playgroud)

javascript jinja2 flask chartist.js

3
推荐指数
1
解决办法
4344
查看次数

尝试在 blog.html 上查找索引号的 DJango 中的正则表达式错误

尝试使用 Django 加载特定索引号(与博客文章相关)时出现以下错误。

错误代码如下- 任何人都可以帮助指出错误吗?

        path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))
Run Code Online (Sandbox Code Playgroud)

该 urls.py 文件上用于显示上下文的完整代码在这里:

from django.urls import path
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post


#it's already going to blog/, so this regular expression is just blank

urlpatterns = [

        path(r'', ListView.as_view(queryset=Post.objects.all().order_by("-date") [:25], 
                                                       template_name="blog/blog.html")),

        path(r'(?P)<pk>\d+)', DetailView.as_view(model = Post,template_name = 'blog/post.html'))
Run Code Online (Sandbox Code Playgroud)

] 我试图访问的 URL 是:

http://127.0.0.1:8000/blog/2
Run Code Online (Sandbox Code Playgroud)

页面上的错误是:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/blog/2
Using the URLconf defined in mysite.urls, …
Run Code Online (Sandbox Code Playgroud)

django url expression

0
推荐指数
1
解决办法
643
查看次数