简体   繁体   中英

Linking Django wepages

I am currently creating a python application using the Django web framework. In the app users can login and create personal goals. These goals are stored in a database and users are able to view them. I am having trouble linking the webpages within the app. When i try to click onto another webpage I am getting an AttributeError telling me that their is know Reverse for 'goal_create' not found. 'goal_create' is not a valid view function or pattern name Reverse for 'goal_create' not found. 'goal_create' is not a valid view function or pattern name This is how I am trying to implement the app:

Models

//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

Views

//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})

Urls

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>

This:

url(r'^create/$', views.goal_create, name='create'),

Should be:

url(r'^create/$', views.goal_create, name='goal_create'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM