Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

html - How to link page from one app to another app

I'm new to django and I'm building a project "myblog" which has "blog" app in which I have created a base.html file which contain nav bar list of About and contact.I also created a "sendemail" app in same "myblog" project and I placed "email.html" contact file in templates directory of "sendemail" app, then what should be the href link in base.html file of "blog" app to access email.html file in "sendemail" app.

This is base.html file in blog app of templates directory.

{% load static %}

<!DOCTYPE html>

<html>
  <head>

    <title>Stand For Christ</title>
  <link rel="stylesheet" href="{% static 'css/base.css' %}">
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:400,700"
      rel="stylesheet">
    <meta name="google" content="notranslate" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
      crossorigin="anonymous"
    />

  </head>

  <body>
    <style>
      body {
        font-family: "Roboto", sans-serif;
        font-size: 17px;
        background-color: #fdfdfd;
      }

    .shadow{
           box-shadow:  0 4px 2px -2px rgba(0,0,0,0.1);

       }



      .btn-danger {
        color: #fff;
        background-color: #1d2671;
        border-color: #1d2671;
      }

     .masthead {
              background: #1d2671;
              height: auto;
              padding-bottom: 15px;
              box-shadow: 0 16px 48px #E3E7EB;
              padding-top: 10px;
    }
    </style>


    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow" id="mainNav">
      <div class="container-fluid">
        <a  class="navbar-brand" href="{% url 'home' %}" style="color:#1d2671;font-size:25px" >Stand For Christ Ministries</a>
        <button
          class="navbar-toggler navbar-toggler-right"
          type="button"
          data-toggle="collapse"
          data-target="#navbarResponsive"
          aria-controls="navbarResponsive"
          aria-expanded="false"
          aria-label="Toggle navigation"
        >
          <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">

            <li class="nav-item text-black">
              <a
                class="nav-link text-black font-weight-bold"
                href="about.html"
                >About</a>
            </li>
            
            <li class="nav-item text-black">
              <a
                class="nav-link text-black font-weight-bold"
                href="#what should be the link here."
                >Contact</a
              >
            </li>
          </ul>
        </div>
      </div>
    </div>
    </nav>


            {% block content %}
          <!-- Content Goes here -->
            {{ content | safe }}
            {% endblock content %}

    <!-- Footer -->
    <footer class="py-3 bg-green">
              <p class="m-0 text-dark text-center ">Copyright &copy; Stand For Christ Ministries</p>
    </footer>

  </body>
</html>

This is urls.py file in "sendemail" app

# sendemail/urls.py
from django.contrib import admin
from django.urls import path

from .views import contactView, successView

urlpatterns = [
    path('contact/', contactView, name='contact'),
    path('success/', successView, name='success'),
]

This is views.py file of "sendemail" app

sendemail/views.py

from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import ContactForm

def contactView(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['[email protected]'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
    return render(request, "email.html", {'form': form})

def successView(request):
    return HttpResponse('Success! Thank you for your message.')

please go to base.html file where contact nav bar link is there and read the comment there,what should be the link there?

question from:https://stackoverflow.com/questions/65872829/how-to-link-page-from-one-app-to-another-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your "sendemail" app urls.py file add before urlpatterns

app_name = 'sendemail'

and then in base.html use

href="{% url 'sendemail:contact' %}"

Follow this to learn more: https://docs.djangoproject.com/en/3.1/topics/http/urls/#reversing-namespaced-urls


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...