我是Python和Django的新手。 我正在观看https://www.youtube.com/watch?v=a48xeeo5Vnk&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p&index=2这门课程并按照说明进行操作。
但是有这样的错误消息不断出现。
AttributeError: module 'blog.views' has no attribute 'post_list'
C:\Users\Administrator\PycharmProjects\django_project\blog\urls.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 398, in check
for pattern in self.url_patterns:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 579, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Administrator\PycharmProjects\django_project\django_project\urls.py", line 6, in <module>
path('blog/', include('blog.urls'))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Administrator\PycharmProjects\django_project\blog\urls.py", line 5, in <module>
path('', views.home, name='post_list'),
AttributeError: module 'blog.views' has no attribute 'home'
在pylint,他们说Module 'blog.views' has no 'home'
因此,我认为apps.py没有home函数,但我已经这样称呼home函数。
def home(request):
return HttpResponse('<h1>Blog Home</h1>')
blog / urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
]
django_project / urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls'))
]
blog / apps.py
from django.apps import AppConfig
from django.http import HttpResponse
def home(request):
return HttpResponse('<h1>Blog Home</h1>')
我找不到问题。
答案 0 :(得分:1)
使用
static void Main(string[] args)
{
string inputFile = args[0];
string inputPlane = args[1];
string inputTime = args[2];
// Check for output flag "-o"
bool checkForOutput = Array.Exists(args, element => element == "-o");
if(!File.Exists(inputFile))
{
Console.WriteLine("Invalid Input");
return;
}
var inputFileLines = GetInputFileFormatted(inputFile);
var splitWords = GetSplitWords(inputFileLines);
var planeFileLines = GetPlaneFileFormatted(inputPlane);
var stationDetails = GetStationDetails(inputFileLines.Count, splitWords);
if (checkForOutput == true)
{
//================== Store station names ==================
string[] names = new string[listLength];
int nameCounter = 0;
while (nameCounter < listLength)
{
for (int i = 0; i < splitWords.Length; i += 3)
{
names[nameCounter] = (splitWords[i]);
nameCounter++;
}
}
//================== Store plane details ==================
Plane plane1 = new Plane(intPlaneSpec[0], intPlaneSpec[1], intPlaneSpec[2], intPlaneSpec[3], intPlaneSpec[4]);
//================== Store distance totals between position [i] & [i + 1] ==================
double[] distanceTotal = new double[listLength];
for (int i = 0; i < stationDetails.Length - 1; i++)
{
distanceTotal[i] = Math.Round(Station.Distance((stationDetails[i].XValue),
(stationDetails[i + 1].XValue),
(stationDetails[i].YValue),
(stationDetails[i + 1].YValue)), 4);
}
int lastStation = stationDetails.GetUpperBound(0);
// Store last distance total
distanceTotal[lastStation] = Math.Round(Station.Distance((stationDetails[lastStation].XValue),
(stationDetails[0].XValue),
(stationDetails[lastStation].YValue),
(stationDetails[0].YValue)), 4);
//================== Setup list for storing times ==================
DateTime startTime = DateTime.Parse(inputTime);
List<DateTime> temporaryTimes = new List<DateTime>();
temporaryTimes.Add(startTime); // Add initial value "23:00
List<string> timeStrings = new List<string>();
for (int i = 0; i < distanceTotal.Length; i++)
{
temporaryTimes.Add(startTime.AddMinutes((Tour.CalTime(distanceTotal[i], plane1))));
string stringVersion = startTime.ToString(@"hh\:mm");
timeStrings.Add(stringVersion);
startTime = (startTime.AddMinutes((Tour.CalTime(distanceTotal[i], plane1))));
}
//================== Store total duration of trip ==================
int last = (temporaryTimes.Count);
TimeSpan totalTime = temporaryTimes[last - 1] - temporaryTimes[0];
//================== Setup list for storing tour names ==================
List<string> tourNames = new List<string>();
//================== Output to Console Window & file ==================
// Setup output file
string outputFile = args[4];
FileStream outFile = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(outFile);
Console.WriteLine("Reading input from {0}", inputFile);
if (totalTime.Days >= 1)
{
Console.WriteLine("Tour time: {0} days {1} hours {2} minutes", totalTime.Days, totalTime.Hours, totalTime.Minutes);
}
else
{
Console.WriteLine("Tour time: {0} hours {1} minutes", totalTime.Hours, totalTime.Minutes);
}
Console.WriteLine("Optimising tour length: Level 1...");
Console.WriteLine("Tour length: {0}", distanceTotal.Sum()); // sum of trip
writer.WriteLine(distanceTotal.Sum()); // write sum to output file
for (int i = 0; i < stationDetails.Length - 1; i++)
{
Console.WriteLine("{0}\t->\t{1}\t{2}",
stationDetails[i].StationName.Substring(0, 2),
stationDetails[i + 1].StationName.Substring(0, 2),
distanceTotal[i]);
// Store names to tour names
tourNames.Add(stationDetails[i].StationName.Substring(0, 2));
tourNames.Add(stationDetails[i + 1].StationName.Substring(0, 2));
writer.WriteLine("{0}\t->\t{1}\t{2}",
stationDetails[i].StationName.Substring(0, 2),
stationDetails[i + 1].StationName.Substring(0, 2),
Math.Round(Station.Distance((stationDetails[i].XValue),
(stationDetails[i + 1].XValue),
(stationDetails[i].YValue),
(stationDetails[i + 1].YValue)), 4));
}
// Write Last distance to console
Console.WriteLine("{0}\t->\t{1}\t{2}",
stationDetails[lastStation].StationName.Substring(0, 2),
stationDetails[0].StationName.Substring(0, 2), distanceTotal[lastStation]);
//================== Store tour Details in object ==================
Tour tour1 = new Tour(4.2, plane1, tourNames, temporaryTimes, totalTime);
////================== Algorithm ==================
//// Empty tour with post office initially added
List<Station> stationsLeft = stationDetails.ToList();
List<Station> fuckingDone = Tour.SimpleHueristic(stationsLeft);
foreach (Station var in fuckingDone)
{
Console.WriteLine("{0} {1} {2}", var.StationName, var.XValue, var.YValue);
}
// Write last distance to output file
writer.WriteLine("{0}\t->\t{1}\t{2}",
stationDetails[lastStation].StationName.Substring(0, 2),
stationDetails[0].StationName.Substring(0, 2),
Math.Round(Station.Distance((stationDetails[lastStation].XValue),
(stationDetails[0].XValue),
(stationDetails[lastStation].YValue),
(stationDetails[0].YValue)), 4));
// Close output file
writer.Close();
outFile.Close();
}
//================== END OF IF STATEMENT - NEXT SECTION OF CODE (NEEDS TO BE THE SAME) ================
else if (checkForOutput == false)
{
//================== Store plane details ==================
Plane plane1 = new Plane(intPlaneSpec[0], intPlaneSpec[1], intPlaneSpec[2], intPlaneSpec[3], intPlaneSpec[4]);
//================== Store time details ==================
DateTime newTime = DateTime.Parse(inputTime);
string formatedTime = newTime.ToString("HH:mm");
//================== Store distance totals between position [i] & [i + 1] ==================
double[] distanceTotal = new double[listLength];
for (int i = 0; i < stationDetails.Length - 1; i++)
{
distanceTotal[i] = Math.Round(Station.Distance((stationDetails[i].XValue),
(stationDetails[i + 1].XValue),
(stationDetails[i].YValue),
(stationDetails[i + 1].YValue)), 4);
}
int lastStation = stationDetails.GetUpperBound(0);
// Store last distance total
distanceTotal[lastStation] = Math.Round(Station.Distance((stationDetails[lastStation].XValue),
(stationDetails[0].XValue),
(stationDetails[lastStation].YValue),
(stationDetails[0].YValue)), 4);
//================== Setup list for storing times ==================
List<DateTime> temporaryTimes = new List<DateTime>();
//================== Setup list for storing tour names ==================
List<string> tourNames = new List<string>();
//================== Output to only Console ==================
Console.WriteLine("Reading input from {0}", inputFile);
Console.WriteLine("Optimising tour length: Level 1...");
Console.WriteLine("Tour length: {0}", distanceTotal.Sum()); // sum of trip
for (int i = 0; i < stationDetails.Length - 1; i++)
{
Console.WriteLine("{0}\t->\t{1}\t{2}",
stationDetails[i].StationName.Substring(0, 2),
stationDetails[i + 1].StationName.Substring(0, 2),
distanceTotal[i]);
}
// Output last distance back to post office
Console.WriteLine("{0}\t->\t{1}\t{2}",
stationDetails[lastStation].StationName.Substring(0, 2),
stationDetails[0].StationName.Substring(0, 2), distanceTotal[lastStation]);
}
else
{
//========== Display error message if input is incorrect ============
Console.WriteLine("Invalid Input");
}
Console.ReadLine();
}
static List<string> GetInputFileFormatted(string inputFile)
{
List<string> lines = File.ReadAllLines(inputFile).ToList(); // Read text and put into list
return lines;
}
static string[] GetSplitWords(List<string> inputFileLines)
{
string words = inputFileLines.Aggregate((i, j) => i + " " + j).ToString(); // Split into sentences
return words.Split(); // Each individual word
}
static List<string> GetPlaneFileFormatted(string inputPlane)
{
List<string> planeLines = File.ReadAllLines(inputPlane).ToList(); // Read text and put into list
string planeElements = planeLines.Aggregate((i, j) => i + " " + j).ToString();
string[] stringPlaneSpec = planeElements.Split();
int[] intPlaneSpec = new int[stringPlaneSpec.Length];
for (int n = 0; n < stringPlaneSpec.Length; n++)
intPlaneSpec[n] = int.Parse(stringPlaneSpec[n]);
return planeLines;
}
static Station[] GetStationDetails(int inputFileLinesLength, string[] splitWords)
{
Station[] stationDetails = new Station[inputFileLinesLength];
int stationCounter = 0;
while (stationCounter < inputFileLinesLength)
{
for (int i = 0; i < splitWords.Length; i += 3)
{
stationDetails[stationCounter] = new Station(splitWords[i], Convert.ToInt32(splitWords[i + 1]), Convert.ToInt32(splitWords[i + 2]));
stationCounter++;
}
}
return stationDetails;
}
您是说视图主页位于模块path('', views.home, name='post_list')
中,但正如您已经说过的那样,它位于apps.py中,因此出现错误:views
。
只需将您的功能/视图Module 'blog.views' has no 'home'
移至blog.views模块。
答案 1 :(得分:0)
您可能必须在设置文件中注册您的应用程序。
您要查找应用列表,然后将应用blog
添加到该列表。