1
看來我被卡住了。我需要你的幫助才能解決這個錯誤。 我認爲URL上的正則表達式是爛的。任何幫助都感激不盡。Django錯誤 - SyntaxError:無效的語法
這是錯誤:
File "/Users/cohen/Documents/project/sanctions/batches/urls.py", line 2, in from . import views File "/Users/cohen/Documents/project/sanctions/batches/views.py", line 23 def detail_businessname(request): ^SyntaxError: invalid syntax
這是我的模型:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
class Batches(models.Model):
BatchNumber = models.IntegerField(null=True)
FileName= models.CharField(max_length=250)
UploadedDate= models.DateField(max_length=250)
UploadedBy = models.CharField(max_length=250)
NumberOfRows= models.IntegerField(null=True)
Hits= models.IntegerField(null=True)
Status= models.CharField(max_length=250)
class Meta:
verbose_name_plural = "Batches"
def __str__(self):
return self.FileName + '- ' + str(self.Hits) + '- ' + str(self.NumberOfRows) + '- ' + self.Status
class BusinessName(models.Model):
AccountingCode = models.CharField(max_length=50)
RefID = models.CharField(max_length=50, default="")
FullName = models.CharField(max_length=250)
Aliases = models.CharField(max_length=250)
Address = models.CharField(max_length=500)
City= models.CharField(max_length=50)
ZipCode= models.IntegerField(null=True)
State = models.CharField(max_length=250)
Country= models.CharField(max_length=250)
TypeOfSanction= models.CharField(max_length=250)
Monitoring= models.CharField(max_length=50)
BatchNumber= models.IntegerField(null=True) # tr pus automat
FileName= models.CharField(max_length=250) # tr pus automat1
UploadDate = models.DateField(max_length=250) # tr pus automat
UploadBy= models.CharField(max_length=250) # tr pus automat
Decision= models.CharField(max_length=250) # tr pus Ulterior
Status= models.CharField(max_length=250) # tr pus automat
EngineDecision= models.CharField(max_length=250) # tr pus automat
WhoAdjudicated= models.CharField(max_length=250)
DateOfAdjudication= models.CharField(max_length=250)
SdnType = models.CharField(max_length=250) #Entity or Individual
class Meta:
verbose_name_plural = "Business Names"
def __str__(self):
return self.FullName + '-' + self.Address + '-' + self.City + '-' + '-' + self.State + '-' + self.Country
class Individuals(models.Model):
AccountingCode = models.CharField(max_length=50)
RefID = models.IntegerField(null=True)
FullName = models.CharField(max_length=250)
FirstName= models.CharField(max_length=250)
Lastname= models.CharField(max_length=250)
DayOfBirth = models.IntegerField(null=True)
MonthOfBirth = models.IntegerField(null=True)
YearOfBirth= models.IntegerField(null=True)
FullDOB = models.DateField(max_length=250)
Aliases = models.CharField(max_length=250)
Address = models.CharField(max_length=500)
City= models.CharField(max_length=50)
ZipCode= models.IntegerField(null=True)
State = models.CharField(max_length=250)
Country= models.CharField(max_length=250)
TypeOfSanction= models.CharField(max_length=250)
Monitoring= models.CharField(max_length=50)
BatchNumber= models.IntegerField(null=True) # tr pus automat
FileName= models.CharField(max_length=250) # tr pus automat
UploadDate= models.DateField(max_length=250) # tr pus automat
UploadBy= models.CharField(max_length=250) # tr pus automat
Decision= models.CharField(max_length=250) # tr pus Ulterior
Status= models.CharField(max_length=250) # tr pus automat
EngineDecision= models.CharField(max_length=250) # tr pus automat
WhoAdjudicated= models.CharField(max_length=250)
DateOfAdjudication= models.CharField(max_length=250)
More= models.CharField(max_length=250)
SdnType = models.CharField(max_length=250) #Entity or Individual
class Meta:
verbose_name_plural = "Individuals"
def __str__(self):
return self.FullName + '-' + self.Address + '-' + self.City + '-' + '-' + self.State + '-' + self.Country
這是我的看法:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from .models import BusinessName
from .models import Individuals
from .models import Batches
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
all_Batches = Batches.objects.all()
html = ''
for batch in all_Batches:
url = '/batches/' + str(batch.id) + '/'
html += '<a href="#"' + url + '">' + str(Batches.BatchNumber)+ '</a><br>'
return HttpResponse(html)
def detail(request, BatchNumber):
return HttpResponse("<h2>Details for Batches ID:" + str(BatchNumber + "</h2")
def detail_businessname(request):
all_BusinessNames = BusinessName.objects.all()
html = ''
for BN in all_BusinessNames:
url = '/businessname/' + str(BusinessName.id) + '/'
html += '<a href="#"' + url + '">' + BusinessName.FullName + '</a><br>'
return HttpResponse(html)
def detail_individuals(request):
return HttpResponse("<h2>Details for Individual Names ID:" + str(Individuals.id) + "</h2")
而這些網址:
from django.conf.urls import url
from . import views
urlpatterns = [
# /batches/
url(r'^$', views.index, name='index'),
# /batches/2
url(r'^(?P<batches_id>[0-9]+)/$',views.detail, name="detail"),
# businessname/1
url(r'^(?P<businessname_id>[0-9]+)/$',views.detail_businessname, name="detail_businessname"),
# individuals/1
url(r'^(?P<individuals_id>[0-9]+)/$', views.detail_individuals, name="detail_individuals"),
謝謝了很多, 科恩
愚蠢的我!謝謝! – Cohen
@JohnCohen如果你得到一個語法錯誤,並不能找出原因,看上一行通常會有所幫助。 – Dunes
謝謝你沙丘!我現在正在編碼時學習! – Cohen