这是我的第一个问题。我正在使用VisualStudio为smartshopping AI创建一个django / python应用程序。这也是我的第一个python / django技术应用程序。我在使用urls.py时遇到问题并且已经读过django版本不包含urlpatterns。我已经更改了我的网址模式以反映在线建议,并更改了我的代码的django.conf.urls导入网址部分。它仍然无法正常工作。请帮忙。
我已经按照网上的建议来到这里:
from datetime import datetime
from django.conf.urls import url
from app.forms import BootstrapAuthenticationForm
# Uncomment the next lines to enable the admin:
from django.conf.urls import include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
# Examples:
url(r'^$', 'app.views.home', name='home'),
url(r'^contact$', 'app.views.contact', name='contact'),
url(r'^about', 'app.views.about', name='about'),
url(r'^login/$',
'django.contrib.auth.views.login',
{
'template_name': 'app/login.html',
'authentication_form': BootstrapAuthenticationForm,
'extra_context':
{
'title':'Log in',
'year':datetime.now().year,
}
},
name='login'),
url(r'^logout$',
'django.contrib.auth.views.logout',
{
'next_page': '/',
},
name='logout'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
]
我想要一个简单的修复,不要改变所有视图来添加包含 - 这些是由visual studio自动生成的。我想保持autogeneraton工作,只需添加一行代码来引用url.py
from django.shortcuts import render
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
def home(request):
"""Renders the home page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/index.html',
context_instance = RequestContext(request,
{
'title':'Home Page',
'year':datetime.now().year,
})
)
def contact(request):
"""Renders the contact page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/contact.html',
context_instance = RequestContext(request,
{
'title':'Contact',
'message':'Your contact page.',
'year':datetime.now().year,
})
)
def about(request):
"""Renders the about page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/about.html',
context_instance = RequestContext(request,
{
'title':'About',
'message':'Your application description page.',
'year':datetime.now().year,
})
)
基于响应和堆栈溢出回答类似的问题(Django URLs error: view must be a callable or a list/tuple in the case of include())我尝试了这种方法(仍然不起作用)。
from datetime import datetime
from django.conf.urls import url
from app.forms import BootstrapAuthenticationForm
from django.contrib.auth import views as auth_views
from SmartShopper import views as SmartShopper_views
# Uncomment the next lines to enable the admin:
# from django.conf.urls import include
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = [
# Examples:
url(r'^$', SmartShopper_views.home, name='home'),
url(r'^contact$', SmartShopper_views.contact, name='contact'),
url(r'^about', SmartShopper_views.about, name='about'),
url(r'^login/$',
'django.contrib.auth.views.login',
{
'template_name': 'app/login.html',
'authentication_form': BootstrapAuthenticationForm,
'extra_context':
{
'title':'Log in',
'year':datetime.now().year,
}
},
name='login'),
url(r'^logout$',
'django.contrib.auth.views.logout',
{
'next_page': '/',
},
name='logout'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
[我的解决方案包含什么 - 我来自asp.net MVC背景和django有点不同,它的MVC类型结构仍然习惯了它,只是帮我运行这个!请帮助。谢谢1
答案 0 :(得分:0)
您无需更改观看次数。问题仍在你的urls.py中。您需要导入视图并直接引用"app.views.home"
,而不是将views.home
引用为字符串。
答案 1 :(得分:0)
通过这个youtube教程了解了更多关于Django框架后,我能够修复我的代码(https://www.youtube.com/watch?v=nAn1KpPlN2w&index=3&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK#t=4.759461)
我学到的是每个“app”都是具有相关视图的程序段。我所要做的就是从my_app或app中导入所有视图,或者在Django中输入你称之为新“app”的任何其他视图。除了从()到[]的urlpatterns稍微改变之外我所做的就是修改url调用以从我上面设置的app_views中拉出来。这是功能代码。我有一个“服务器错误(500)”这是我要弄清楚的其他东西,但是请为那些刚刚接触django框架的开发人员解释更多内容。 Visualstudio是一个很好的工具。用它来做python。如果您有任何问题,请联系我,我很乐意帮助新手。这是我的功能代码。
from datetime import datetime
from django.conf.urls import url
from app.forms import BootstrapAuthenticationForm
from django.contrib.auth import views as auth_views
from app import views as app_views
# Uncomment the next lines to enable the admin:
# from django.conf.urls import include
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = [
# Examples:
url(r'^$', app_views.home, name='home'),
url(r'^contact$', app_views.contact, name='contact'),
url(r'^about', app_views.about, name='about'),
url(r'^login/$',
auth_views.login,
{
'template_name': 'app/login.html',
'authentication_form': BootstrapAuthenticationForm,
'extra_context':
{
'title':'Log in',
'year':datetime.now().year,
}
},
name='login'),
url(r'^logout$',
auth_views.logout,
{
'next_page': '/',
},
name='logout'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
]