简体   繁体   中英

Ref doesn't work correctly with my settings

I'm learning to make some chat website, and i cannot quite get how to use urls correctly. The problem is that when I get to "http://127.0.0.1:8000/chat", links into the header became like "http://127.0.0.1:8000/chat/main" instead of "http://127.0.0.1:8000/main"

project urls:

from django.contrib import admin
from django.urls import path
from django.urls import include, path
from chat import views as chat_views
from mainapp import views as mainapp_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', mainapp_views.index),
    path('chat/', include('chat.urls')),

]

chat app urls:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<str:room_name>/', views.room, name='room'),

]

chat app views:

from django.shortcuts import render

def index(request):
    return render(request, 'chat/chatindex.html')

def room(request, room_name):
    return render(request, 'chat/room.html', {
        'room_name': room_name
    })

index.html

<!DOCTYPE html>
{% load static %}
<html>
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
    <title>{% block title %}{% endblock title %}</title>

</head>
<body>
<header>
    <div class="navbar navbar-dark bg-dark shadow-sm">
        <a href="" class="navbar-brand d-flex align-items-center">Главная</a>
        <a href="searching" class="navbar-brand d-flex align-items-center">Поиск собеседников</a>
        <a href="contacts" class="navbar-brand d-flex align-items-center">Контакты</a>
        <a href="faq" class="navbar-brand d-flex align-items-center">FAQ</a>
        <a href="login" class="navbar-brand d-flex align-items-center">Регистрация/Вход</a>
    </div>
    </div>
</header>
<main>
<div>{% block content%}{% endblock content %}</div>
</main>
<footer class="footer mt-auto py-3 bg-light">
  <div class="container">
    <span class="text-muted">Давай общаться!</span>
  </div>
</footer>

</body>
</html>

chatindex.html

{% extends "index.html" %}
{% block title %}Контакты{% endblock title %}
{% block header %}{% endblock header %}
{% block content %}
В какую комнату хотите зайти?
<br>
<input id="room-name-input" type="text" size="100">
<br>
<input id="room-name-submit" type="button" value="Enter">

<script>
        document.querySelector('#room-name-input').focus();
        document.querySelector('#room-name-input').onkeyup = function(e) {
            if (e.keyCode === 13) {  // enter, return
                document.querySelector('#room-name-submit').click();
            }
        };

        document.querySelector('#room-name-submit').onclick = function(e) {
            var roomName = document.querySelector('#room-name-input').value;
            window.location.pathname = '/chat/' + roomName + '/';
        };



</script>

{% endblock content%}   

I'm pretty sure that the problem is about " path('<str:room_name>/', views.room, name='room') ", but i can't understand how to fix it and save opportunity to enter rooms with chat-page. Thanks a lot!

Very likely you make relative paths. Indeed, if you visit a page like some.domain.com/foo , and the link is <a href="bar">link</a> , then it will visit some.domain.com/foo/bar , since that is a relative path: relative to the current path.

You can solve this by specifying an absolute path, which has a leading slash, so:

<!--     ↓ leading slash -->
<a href="/faq" class="navbar-brand d-flex align-items-center">FAQ</a>

But it is better not to generate URLs manually. If you later for example change the URL paths, you will need to alter all URLs. You can give your views a name, like:

path('faq', views.faq)

and in the template work with the {% url … %} template tag [Django-doc] :

<!--     ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ url template tag -->
<a href="" class="navbar-brand d-flex align-items-center">FAQ</a>

Django will then look for a view with the given name, and automatically generate the corresponding URL.

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