My url template not work
Question: I have a problem with my template tag url. The redirect not work when i click
on button.

Django version => 1.9 Python version => 2.7

In my urls.py(main) i have:

    
    
    from django.conf import settings
    from django.conf.urls import include, url
    from django.conf.urls.static import static
    from django.contrib import admin
    from memoryposts.views import home, profile, preregistration
    
    urlpatterns = [
        url(r'^$', home, name="home"),
        url(r'^grappelli/', include('grappelli.urls')),
        url(r'^admin/', admin.site.urls),
        url(r'^memory/', include("memoryposts.urls", namespace="memory")),
        url(r'^avatar/', include('avatar.urls')),
        url(r'^accounts/', include('registration.backends.hmac.urls')),
        url(r'^preregistration/', preregistration, name="preregistration"),
        url(r'^profile/', profile, name="profile"),
    ]
    
    if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

In my urls.py(apps) i have:

    
    
    from django.conf.urls import url
    from django.contrib import admin
    from .views import (
        memory_list,
        memory_create,
        memory_detail,
        memory_update,
        memory_delete,
    )
    
    urlpatterns = [
        url(r'^$', memory_list, name='list'),
        url(r'^create/$', memory_create, name='create'),
        url(r'^(?P<slug>[-\w]+)/$', memory_detail, name='detail'),
        url(r'^(?P<slug>[-\w]+)/edit/$', memory_update, name='update'),
        url(r'^(?P<slug>[-\w]+)/delete/$', memory_delete, name='delete'),
    ]
    

In my views.py(apps) i have:

    
    
    from django.contrib import messages
    from django.http import HttpResponse, HttpResponseRedirect
    from django.shortcuts import render, get_object_or_404, redirect
    from .models import Post
    from .forms import PostForm
    
    def home(request):
        return render(request, "base.html")
    
    def profile(request):
        return render(request, "profile.html")
    
    def preregistration(request):
        return render(request, "preregistration.html")
    
    def memory_create(request):
    
        form = PostForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            messages.success(request,"Succès !")
            return HttpResponseRedirect(instance.get_absolute_url())
        context = {
            "form": form,
        }
        return render(request, "memory_create.html", context)
    
    def memory_detail(request, slug=None):
        instance = get_object_or_404(Post, slug=slug)
        context = {
            "title":instance.title,
            "instance":instance,
        }
        return render(request, "memory_detail.html", context)
    
    def memory_list(request):
        queryset = Post.objects.all()
        context = {
            "object_list": queryset,
        }
        return render(request, "memory_list.html", context)
    
    def memory_update(request, slug=None):
    
            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,"Mis à jour !")
                return HttpResponseRedirect(instance.get_absolute_url())
    
            context = {
                "title":instance.title,
                "instance":instance,
                "form": form,
            }
            return render(request, "memory_create.html", context)
    
    def memory_delete(request, slug=None):
        instance = get_object_or_404(Post, slug=slug)
        instance.delete()
        messages.success(request, "Supprimer !")
        return redirect("posts:list")
    

In my template html i have:

    
    
    <button type="button" class="btn btn-primary"><a id="back-profile" href="{% url 'memory:update' %}"> Update</a></button>
    
    <button type="button" class="btn btn-primary"><a id="back-profile" href="{% url 'memory:delete' %}"> Delete</a></button>
    

The redirect not work with this template tag. can you help me please :) ?


Answer: From doc here [URL
dispatcher](https://docs.djangoproject.com/en/1.10/topics/http/urls/#url-
dispatcher)

    
    
    <button type="button" class="btn btn-primary"><a id="back-profile" href="{% url 'memory:update' %}"> Update</a></button>
    
    <button type="button" class="btn btn-primary"><a id="back-profile" href="{% url 'memory:delete' %}"> Delete</a></button>
    

You should put what you 'slug' in your button, like this (if slug is 200)

    
    
    <button type="button" class="btn btn-primary"><a id="back-profile" href="{% url 'memory:update' 200 %}"> Update</a></button>
    

Usually will look like this:

    
    
    {% for slug in slug_list %}
    <button type="button" class="btn btn-primary"><a id="back-profile" href="{% url 'memory:update' slug %}"> Update</a></button>
    {% endfor %}
    

