title: Unable to link Blog Posts to Specific Category Django
body: <p>I used Tango with Django to create a database containing categories, and I used the Django Girls tutorial to add a blog to the database. Both are working fine, but I have been having trouble linking each blog post to its respective category. Right now, all posts go to my post_list.html page.</p>
  
  <p>If I were doing this from scratch, I would add a new view, add a new template, add a url mapping, and then add a link from the category page. I also am aware that I need to edit my blog post model to contain:</p>
  
  <pre><code>category - models.ForeignKey(Category)
  </code></pre>
  
  <p>I thought it would be as simple as adding this to my url.py:</p>
  
  <pre><code>url(r'^category/(?P&lt;category_name_slug&gt;[\w\-]+)/post_edit/$', views.add_page, name='add_page'),  
  </code></pre>
  
  <p>However after a few rounds of trying things along these lines, I can't quite get anything to work. I run into error after error. I have tried going through other blog-related tutorials online, and looking at other stack-overflow posts related to things like this, but I am still stuck. I am a newbie to Django; I am pretty sure that's playing a roll in my lack of success too.</p>
  
  <p>Below is the (unmodified) code I have so far. Can someone help me with this? </p>
  
  <p>My models.py</p>
  
  <pre><code>class Post(models.Model):
  author = models.ForeignKey('auth.User')
  title = models.CharField(max_length=200)
  text = models.TextField()
  created_date = models.DateTimeField(
          default=timezone.now)
  published_date = models.DateTimeField(
          blank=True, null=True)
  
  def publish(self):
      self.published_date = timezone.now()
      self.save()
  
  def __str__(self):
      return self.title
  
  class Category(models.Model):
      name = models.CharField(max_length=128, unique=True)
      views = models.IntegerField(default=0)
      likes = models.IntegerField(default=0)
      slug = models.SlugField()
  
      def save(self, *args, **kwargs):
              # Uncomment if you don't want the slug to change every time the name changes
              #if self.id is None:
                      #self.slug = slugify(self.name)
              self.slug = slugify(self.name)
              super(Category, self).save(*args, **kwargs)
  
      def __unicode__(self):  #For Python 2, use __str__ on Python 3
          return self.name
  </code></pre>
  
  <p>My urls.py</p>
  
  <pre><code>    url(r'^category/(?P&lt;category_name_slug&gt;[\w\-]+)/$', views.category, name='category'),
      url(r'^post_list/$', views.post_list, name='post_list'),
      url(r'^post/(?P&lt;pk&gt;[0-9]+)/$', views.post_detail, name='post_detail'),
      url(r'^post/new/$', views.post_new, name='post_new'),
      url(r'^post/(?P&lt;pk&gt;[0-9]+)/edit/$', views.post_edit, name='post_edit'),
  </code></pre>
  
  <p>My views.py</p>
  
  <pre><code> def post_list(request):
   posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
   return render(request, 'rango/post_list.html', {'posts': posts})
  
  def post_detail(request, pk):
  post = get_object_or_404(Post, pk=pk)
  return render(request, 'rango/post_detail.html', {'post': post})
  
  def post_new(request):
  if request.method == "POST":
      form = PostForm(request.POST)
      if form.is_valid():
          post = form.save(commit=False)
          post.author = request.user
          post.published_date = timezone.now()
          post.save()
          return redirect('post_detail', pk=post.pk)
  else:
      form = PostForm()
  return render(request, 'rango/post_edit.html', {'form': form})
  
  def post_edit(request, pk):
  post = get_object_or_404(Post, pk=pk)
  if request.method == "POST":
      form = PostForm(request.POST, instance=post)
      if form.is_valid():
          post = form.save(commit=False)
          post.author = request.user
          post.published_date = timezone.now()
          post.save()
          return redirect('post_detail', pk=post.pk)
  else:
      form = PostForm(instance=post)
  return render(request, 'rango/post_edit.html', {'form': form})
  </code></pre>
  
  <p>Thanks in advance.</p>