Есть ли лучший способ перемещаться между взглядами Джанго помимо вставки нового URL? - PullRequest
0 голосов
/ 11 января 2019

Я учусь использовать Django для создания своего веб-приложения. Я хочу получить доступ к другой странице по ссылке на странице индекса, но браузер продолжает добавлять файл и имя приложения в запрос. Как заставить браузер переключаться между ссылками, не добавляя имя каталога каждый раз?

Я пытался использовать reg exp со старым методом url, но, похоже, он не работает

#   My project
#   urls.py     
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('index.urls')),


]
#  My app
#  urls.py
urlpatterns = [
    path('index/',include([
        path('', views.index,name="index"),
        path('theteam', views.theteam,name="theteam"),
        path('services',views.services,name="services"),
        path('quotes',views.quotes,name="quotes"),
        path('insurance',views.insurance,name="insurance"),
        path('contact',views.contact,name="contact"),
        path('thanks', views.thanks,name="thanks"),

        ])),


]

# Views
def index(request):
    return render(request, 'index/index.html')

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

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

def quotes(request):
    form = ContactForm(request.POST or None)
    if request.method == 'POST':
        return redirect(request, '/thanks/')

    return render(request, 'index/quotes.html',  { 'form': form })

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

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

def thanks(request):
    return render(request, '/thanks.html')

#My Views HTML
 <div class="menu-bar ">
     <ul>
           <a href="{% url 'services' %}"> <li>Services</li></a>
           <a href="{% url 'theteam' %}"><li>The Team</li> </a>
           <a href="{% url 'quotes' %}"><li>Quotes</li> </a>
           <a href="{% url 'insurance' %}"> <li>Insurance</li></a>
           <a href="{% url 'contact' %}"><li>Contact Us</li></a>
     </ul>
     </div>

До сих пор мне удавалось посещать каждую страницу, просто вставив "/ index / template /" в браузер, но я не могу переключаться между ссылками. Мой ожидаемый результат - возможность переключения между страницами с помощью ссылок.

1 Ответ

0 голосов
/ 11 января 2019

В ваших приложениях urls.py добавьте строку над urlpatterns, которая выглядит следующим образом:

app_name = your_app_name

Затем в вашем HTML-файле измените

{% url 'services' %}

до

{% url 'your_app_name:services' %}
...