dsbonafe
dsbonafe

Reputation: 315

Return and link to homepage django

I have a project in DJANGO with this structure:

/
|---- core
|---- client

In client/views.py, I have the code:

class ClientDelete(DeleteView):
    model = Cliente
    success_url = reverse_lazy('cliente_list')

Where client_list is the HTML page on client/clients that lists all clients.

In core/views.py module, I have the function:

def homepage(request):
    return render(request, 'home.html')

Where "home.html" is the homepage. My main urls.py is something like this:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^cliente/', include('clientes.urls')),
    url(r'^about/', aboutpage),
    url(r'^$', homepage),
]

I need to return and make a link into client, core, and other apps to homepage. But when I try to return homepage into client.views.ClientDelete, the url on browser didn't redirect to home, but shows something like:

localhost:8000/client/home when I want back to localhost:8000.

What should I do?

Thank you.

Upvotes: 0

Views: 3959

Answers (1)

Andrii Rusanov
Andrii Rusanov

Reputation: 4606

First, it is useful to set names for urls:

url(r'^$', homepage, name='home')

So for your code you should do:

class ClientDelete(DeleteView):
    model = Cliente
    success_url = reverse_lazy('home')

if you want to be redirected to home page. You can also use namespaces if you have different apps. So you will be able to do:

# redirect to home
success_url = reverse_lazy('home')
# redirect to clients list
success_url = reverse_lazy('clients:list')

To use it you need to make following changes:

url(r'^cliente/', include('clients.urls', namespace='clients'))

and set a name for urls inside clientes.urls.

Docs: https://docs.djangoproject.com/es/1.9/topics/http/urls/#url-namespaces-and-included-urlconfs

Upvotes: 3

Related Questions