我目前正在使用Django Web框架创建一个python应用程序。在应用程序中,用户可以登录并创建个人目标。这些目标存储在数据库中,用户可以查看它们。我无法在应用内链接网页。当我尝试点击另一个网页时,我收到一个AttributeError,告诉我他们知道Reverse for 'goal_create' not found. 'goal_create' is not a valid view function or pattern name
这就是我尝试实现该应用程序的方式:
模型
//The information that i am storing in the DB
class Goal(models.Model):
title = models.CharField(max_length=1000)
body = models.TextField()
created_data = models.DateTimeField(auto_now_add=True, auto_now=False)
updated_data = models.DateTimeField(auto_now_add=False, auto_now=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
视图
//the list of goals that the User will see on the webpage.
def goal_list(request):
goals = Goal.objects.all().order_by('created_data')
return render(request, 'goals/goal_list.html', { 'goals': goals })
@login_required
def goal_create(request, *args):
if request.method == 'POST':
form = forms.CreateGoal(request.POST, request.FILES)
if form.is_valid():
goal_create = Post(CreateGoal.title.data, CreateGoal.text.data)
# save article to db
//Returns the list of goals after it has been created.
return redirect('goals:goal_list')
else:
form = forms.CreateGoal()
return render(request, 'goals/goal_create.html', {'form': form})
网址
app_name = 'goals'
urlpatterns = [
url(r'^$', views.goal_list, name='list'),
url(r'^create/$', views.goal_create, name='create'),
url(r'^details/$', views.goal_detail, name="goal_detail"),
base.html文件
//My navbar that I'm using to link Webpages.
<a class="navbar-brand" href="/">RecoverRight</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
{% if user.is_authenticated %}
<ul class="nav navbar-nav">
<li><a href="{% url 'account:profile' %}">Profile</a></li>
<li><a href="{% url 'account:edit_profile' %}">Edit Profile</a></li>
<li><a href="{% url 'goals:goal_create' %}">Add Goal</a></li>
<li><a href="{% url 'goals:goal_list' %}">View Goals</a></li>
<li><a href="{% url 'nutrition:nutrition' %}">Nutrition</a></li>
<li><a href="{% url 'nutrition:exercise' %}">Exercise</a></li>
{# <li><a href="{% url 'workout:workout' %}">Workout</a></li>#}
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="{% url 'account:logout' %}">Log out</a></li>
</ul>
答案 0 :(得分:2)
此:
url(r'^create/$', views.goal_create, name='create'),
应该是:
url(r'^create/$', views.goal_create, name='goal_create'),