Django redirect call appends package name to url

I am writing a view for editing a title in django (at 'project_edit.html', and redirecting to the 'project_detail.html' page, but (loosely) following the tutorial here I'm using the redirect method:

def project_edit(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    if request.method == "POST":
        form = ProjectForm(request.POST)
        if form.is_valid():
            project_update = form.save(commit=False)
            project.title = project_update.title
            project.save()
            return redirect(to='gantt_charts.views.project_detail', kwargs={'pk', project.pk}, permanent=True)
        else:
            messages.error(request, "Form invalid!")
            return render(request, 'project_edit.html', {'project':project, 'form':form})
    else:
        form = ProjectForm()
    return render(request, 'project_edit.html', {'project':project, 'form':form})

In the tutorial, they also use redirect:

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.save()
            return redirect('blog.views.post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})

But when I click my link http://localhost:8000/project/2/edit and submit my form, I get sent to: http://localhost:8000/project/2/gantt_charts.views.project_detail

I cannot fathom why.

Looking at the redirect definition in shortcuts.py and then the resolve_url definition, my only guess is that it drops to the bottom and it just returned as is. Why isn't the lookup performed?

ed:

Here are my url patterns for that app:

urlpatterns = patterns('', 
    url(r'^(?:project)?/?$', views.project_list), 
    url(r'^project/(?P<project_id>\d+)/edit$', views.project_edit), 
    url(r'^project/(?P<project_id>\d+)/[A-z\\-]{0,50}$', views.project_detail), 
    )

Answers


Add a name to your project detail url:

url(r'^project/(?P<project_id>\d+)/[A-z\\-]{0,50}$',
                       views.project_detail, name='project_detail`),

And then refer to this name in redirect() call:

return redirect('project_detail', project_id=project.pk, permanent=True)

Need Your Help

SQL Count Age between two date range

sql-server datetime count range

I'm trying to count the age of my active records from the database from the current date to a possible past date.

glassfish: redirect all url to index

jsf java-ee glassfish glassfish-3

I'm working on the java ee application using glassfish 3 server and jsf.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.