Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Turn python slug url into link in HTML. (Django)

I'm having issues turning this slug url pattern into a link in my html. I have delete and edit methods in my views.py and they work as needed if I type them in...example: http://127.0.0.1:8000/posts/football-banner/delete will delete the post and http://127.0.0.1:8000/posts/football-banner/update will allow me to edit the post. I just need to get update and delete into a link in my html. Can anyone suggest a solution to this? Thanks.

html

{% if request.user.is_authenticated %}
    <a href="{% url "posts:update" %}" class="delete btn btn-success">Edit</a>
    <a href="{% url "posts:delete" %}" class="delete btn btn-danger" data-confirm="Are you sure to delete this item?">Delete</a>
{% else %}
        <p >Cannot Edit Posts</p>
        {% endif %}

urls.py

from .views import (
    post_list,
    post_create,
    post_detail,
    post_update,
    post_delete,
    )

urlpatterns = [
    url(r'^$', post_list, name='list'),
    url(r'^create/$', post_create, name='create'),
    url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
    url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
    url(r'^(?P<slug>[\w-]+)/delete/$', post_delete, name='delete'),
    #url(r'^posts/$', "<appname>.views.<function_name>"),
]

views.py

def post_update(request, slug=True):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    instance = get_object_or_404(Post, slug=slug)
    form = PostForm(request.POST or None, request.FILES or None, instance=instance)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, "<a href='#'>Item</a> Saved", extra_tags='html_safe')
        return HttpResponseRedirect(instance.get_absolute_url())

    context = {
        "title": instance.title,
        "instance": instance,
        "form": form,
    }
    return render(request, "post_form.html", context)


def post_delete(request, slug=None):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    instance = get_object_or_404(Post, slug=slug)
    instance.delete()
    messages.success(request, "Successfully deleted")
    return redirect("posts:list")

1 Answer

Both the views that you're calling in the HTML take a slug keyword. I rather doubt that you have posts with the slugs of True or None and, as that's the case, you will get a 404 error when you try to click on the links as they stand.

Assuming that you have a post in the context_data for the page that these links are on, you can try something like this. It might fix your errors.

{% if request.user.is_authenticated %}
    <a href="{% url 'posts:update' slug=post.slug %}" class="delete btn btn-success">Edit</a>
    <a href="{% url 'posts:delete' slug=post.slug %}" class="delete btn btn-danger" data-confirm="Are you sure to delete this item?">Delete</a>
{% else %}
    <p>Cannot Edit Posts</p>
{% endif %}