简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM