几个星期以来,我一直在尝试学习Django,但现在我完全迷路了。我对正则表达式不太了解,也看不出我的错误在哪里。
我构建了一个在不同班级领域进行研究的应用程序。我用模板和URL创建了第一个视图(view.index_cleint)。在学习使用正则表达式管理URL的方式中,我决定使用其他查询过滤器制作另一个视图(view.test_api)。 (我有2个模板链接到2个不同的视图,以显示每个正确的信息。)
这是带有正则表达式的URL文件:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('search/', views.index_client, name='dashboard'),
re_path('^search/(?P<param>\w+)/$', views.index_client, name='dashboard'),
path('dash/', views.test_api, name='test_dash'),
re_path('^dash/(?P<param>\w+)/$', views.test_api, name='test_dash'),
]
前两个URL用于我的视图1并处理搜索表单。 倒数第二个是我的第二个观点,用于处理搜索表单。
这是我的第一个视图的代码:
def index_client(request, param='company_name'):
print("test all")
if request.method == 'GET' and 'q' in request.GET:
query = request.GET['q']
if query is None or query == '':
query = ''
else:
query = ''
list_param_query = ['company_name', 'email', 'status', 'id']
if param not in list_param_query:
param = 'company_name'
url_param = param + '/'
try:
last_update_obj = UpdateDatabase.objects.latest('last_update')
except UpdateDatabase.DoesNotExist:
diff_date = None
hours = 0
minutes = 0
seconds = 0
print("No last_update in database")
else:
last_update = last_update_obj.last_update
date_now = timezone.now()
diff_date = date_now - last_update
days, seconds = diff_date.days, diff_date.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
try:
customer_list = Client.objects.filter(
Q(company_name__contains=query) | Q(email__contains=query)
| Q(status__contains=query) | Q(first_name__contains=query)
| Q(last_name__contains=query)).order_by(str(param))
print(customer_list)
except Client.DoesNotExist:
return render(request, 'maintenance/dashboard.html', locals())
print(customer_list.count())
page = request.GET.get('page', 1)
paginator = Paginator(customer_list, 1000)
try:
customers = paginator.page(page)
except PageNotAnInteger:
customers = paginator.page(1)
except EmptyPage:
customers = paginator.page(paginator.num_pages)
try:
total_confirmed = Client.objects.filter(status="CONFIRMED").count()
except Client.DoesNotExist:
total_confirmed = 0
print("Error can't count customers")
if diff_date is not None:
return render(request, 'maintenance/dashboard.html', {'customers': customers,
'total_confirmed': total_confirmed,
'diff_date': diff_date, 'hours': hours,
'minutes': minutes, 'seconds': seconds})
return render(request, 'maintenance/dashboard.html', {'customers': customers,
'total_confirmed': total_confirmed,
'diff_date': diff_date})
这是我第二个视图的代码:
def test_api(request, param='company_name'):
print("test2")
if request.method == 'GET' and 'q' in request.GET:
query = request.GET['q']
if query is None or query == '':
query = ''
else:
query = ''
list_param_query = ['company_name', 'email', 'status', 'id']
if param not in list_param_query:
param = 'company_name'
url_param = param + '/'
try:
last_update_obj = UpdateDatabase.objects.latest('last_update')
except UpdateDatabase.DoesNotExist:
diff_date = None
hours = 0
minutes = 0
seconds = 0
print("No last_update in database")
else:
last_update = last_update_obj.last_update
date_now = timezone.now()
diff_date = date_now - last_update
days, seconds = diff_date.days, diff_date.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
try:
search_1 = Q(status__contains="CONFIRMED")
customer_list = Client.objects.filter(search_1 &
Q(company_name__contains=query) | Q(email__contains=query)
| Q(status__contains=query) | Q(first_name__contains=query)
| Q(last_name__contains=query)).order_by(str(param))
print(customer_list)
except Client.DoesNotExist:
return render(request, 'maintenance/dashboard_p1.html', locals())
print(customer_list.count())
page = request.GET.get('page', 1)
paginator = Paginator(customer_list, 1000)
try:
customers = paginator.page(page)
except PageNotAnInteger:
customers = paginator.page(1)
except EmptyPage:
customers = paginator.page(paginator.num_pages)
try:
total_confirmed = Client.objects.filter(status="CONFIRMED", priority=1).count()
except Client.DoesNotExist:
total_confirmed = 0
print("Error can't count customers")
if diff_date is not None:
return render(request, 'maintenance/dashboard_p1.html', {'customers': customers,
'total_confirmed': total_confirmed,
'diff_date': diff_date, 'hours': hours,
'minutes': minutes, 'seconds': seconds})
return render(request, 'maintenance/dashboard_p1.html', {'customers': customers,
'total_confirmed': total_confirmed,
'diff_date': diff_date})
如果这是我用于视图的模型:
class Client(models.Model):
company_name = models.CharField(max_length=100)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(max_length=70, unique=True)
is_confirmed = models.BooleanField(default=False)
is_open = models.BooleanField(default=False)
priority = models.IntegerField(default=0)
date_open = models.DateTimeField(null=True, blank=True)
date_confirmed = models.DateTimeField(null=True, blank=True)
CONFIRMED = 'CONFIRMED'
WAITING = 'WAITING'
OPEN = 'OPEN'
MNT_STATUS = [
(CONFIRMED, 'Confirmed'),
(OPEN, 'Open'),
(WAITING, 'Waiting'),
]
status = models.CharField(
max_length=11,
choices=MNT_STATUS,
default=WAITING,
)
第一个视图和第二个视图之间的唯一区别是一个过滤器,该过滤器可将所有优先级为1的客户都获得一个过滤器
在第一个视图中:
try:
customer_list = Client.objects.filter(
Q(company_name__contains=query) | Q(email__contains=query)
| Q(status__contains=query) | Q(first_name__contains=query)
| Q(last_name__contains=query)).order_by(str(param))
print(customer_list)
except Client.DoesNotExist:
return render(request, 'maintenance/dashboard.html', locals())
在第二个视图中:
try:
search_1 = Q(status__contains="CONFIRMED")
customer_list = Client.objects.filter(search_1 &
Q(company_name__contains=query) | Q(email__contains=query)
| Q(status__contains=query) | Q(first_name__contains=query)
| Q(last_name__contains=query)).order_by(str(param))
print(customer_list)
except Client.DoesNotExist:
return render(request, 'maintenance/dashboard_p1.html', locals())
在创建第二个视图test_api并添加两个lasts URL之前,一切正常,在单击搜索表单时,我被重定向到视图1,并且搜索成功。
但是由于每次我从视图2的模板进行研究时都会添加第二个视图,因此显示了视图1查询的结果。我不会只获得优先级1,而是获得所有对象
当我从“ 127.0.0.1:8000/dash /”(视图2)进行研究时,我看到了printf(2),这是我的第二个视图,但是查询不受尊重并且得到了结果从视图1开始。
您看到我犯了任何错误吗?
提前发送!
答案 0 :(得分:0)
一切似乎都正确。我认为问题出在search_1 = Q(status__contains="CONFIRMED")
。
请向我们展示您的客户模型。 (对不起,我无法发表评论)