linking to the index.html to other html page in Django - django

I am new to the Django project.
I have been trying to create a situation in which, when a user presses the "Call us now" button in my nav-bar, it links them to other html pages (here: contact.html). How can I adjust what I have to achieve this?
Here are my files:
index.html
code: </ul><button class="btn btn-primary" type="button">call us now!</button></div>
views.py
from django.shortcuts import render
def index(request):
return render(request, 'jobs/index.html')
def contacts(request):
return render(request, 'jobs/contact.html')
in the urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.index, name='index'),
path('contact/', jobs.views.contacts, name='contact'),] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)**

Change
href="contact.html"
To :
href="{% url 'contact' %}"

Related

Django URLs Reverse working but not updating my URL

I'm new to Django 4.0.4.
I'm trying to use reverse in model to dynamically change the url without affecting other branch not affecting.
url.py:
urlpatterns = [
path('', home_view, name='home'),
path('products/', product_list, name='product_list'),
path('products/<int:myid>/', dynamic_lookup_view, name='product-detail'),
path('admin/', admin.site.urls),
]
models.py
def get_absolute_url(self):
return reverse("product-detail", kwargs={"myid": self.id})
html
<p>
{{instance.id}} {{instance.title}}
</p>
Output(working):
enter image description here
enter image description here
Problem:
when i change root url for dynamic_lookup_view from 'products/int:myid/' to 'ps/int:myid/' in url.py
path('products/', product_list, name='product_list'),
path('p/<int:myid>/', dynamic_lookup_view, name='product-detail'),
There is no update in my instance.get_absolute_url in my html!?

Django duplicated url request: "home/home/'

I have code that works perfectly on localhost, but it is giving an error when placed on the server.
The point is that when I upload a file, it returns "base.com/home/home" (home twice = 404) instead of just "base.com/home" or the destination of the redirect.
Template:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="myfile">
<button type="submit">Upload</button>
</form>
View:
def login_page(request):
if request.user.is_authenticated:
return redirect('base-home')
form = UserLoginForm(request.POST or None)
nextr = request.GET.get('next')
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
login(request, user)
if nextr:
return redirect(nextr)
return redirect('base-home')
return render(request, 'base/login.html', {'form': form})
#login_required
def home(request):
if request.method == 'POST':
return redirect('base-temp', 1)
return render(request, 'base/home.html', {'test': 'test'})
Url:
urlpatterns = [
path('', views.login_page, name='base-login'),
path('logout/', views.logout_page, name='base-logout'),
path('home/', views.home, name='base-home'),
path('temp/<int:pk>/', views.temp, name='base-temp')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Project settings:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
All other pages, media, statics are working fine. Only the post in this view.
I tried using action with ".", "/home/" and "{% url 'base-home'%}", but they all give the same 404 for duplicating the home url.
I also tried to create a separate view to handle the upload, but the error became "home/upload" instead of "upload".
I saw some similar questions in the stackoverflow, but I didn't find an answer that would work for me.
Any suggestion?
EDIT:
It works if I change the template to:
<form method="post">
{% csrf_token %}
<textarea name="text"></textarea>
<button type="submit">Send</button>
</form>
And the view to:
def home(request):
if request.method == 'POST':
return redirect('base-temp', 2)
return render(request, 'base/home.html', {'test': 'test'})
I'm not using django's forms.py.
That's it. Nothing else. Only the imports and a simple function view/template for base-temp.
Edit 2: Added more details about the views.
SOLUTION:
In case anyone else has the same problem, I leave the solution here, based on Maxwell O. Oyaro's answer.
I changed the home route to the empty path of the app urls.py:
urlpatterns = [
path('', views.home, name='base-home'),
path('logout/', views.logout_page, name='base-logout'),
path('temp/<int:pk>/', views.temp, name='base-temp')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Then, I moved login path to the project urls.py:
...
from base import views as base_views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', base_views.login_page, name='login'),
path('', include('base.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Check if your home is defined globaly for the django project or it is for the app itself.
Putting templates globally for the project and for the app alone can be confusing. If you have a templates for the project with home and you have another home template in the app calle home, it may not give you what you expect.
Just check keenly.

url getting doubled when opening a link from the home page.(django)

On clicking the 2nd item in the list it opens the events.html page but the url shows:
http://127.0.0.1:8000/events/events/ instead of http://127.0.0.1:8000/events
Home page code:
<li>HOME</li>
<li>EVENTS</li>
<li>REGISTERED TEAMS</li>
<li>CONTACT US</li>
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_page, name='home_page'),
path('hackathon_register/',views.hackathon_register,name='hackathon_register'),
path('events/',views.events,name='events')
]
views.py
def events(request):
return render(request,'testapp/events.html',{})
From here, it seems that your code in urls.py of project is:
.......
urlpatterns = [
....
path('events/', include("app.urls")),
]
So correct this mistake either by changing project.urls or app.urls.

django form action url 'abc' is redirect to abc twice

In django index.html, I have below code
<form class="selectedPizza" action="{% url 'cost' %}" method="POST">
<!--Do some action here-->
<form>
In my app(order) urls.py
from django.urls import
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("cost/", views.cost, name="cost")
]
In main site(pizza) urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("", include("orders.urls")),
path("cost/", include("orders.urls")),
path("admin/", admin.site.urls),
]
In views.py
def index(request):
#some action here
return render(request, 'orders/index.html', context)
def cost(request):
if (request.method == 'POST'):
#some action here
return render(request, 'orders/cost.html', context)
After submitting the form I am redirected to "http://127.0.0.1:8000/cost/cost/".
I am learning Django and not able to find a possible way to get this form to redirect to cost.html page after submitting
I don't know why you have included "orders.urls" twice in your main urls. Remove the second one. "cost" is part of the included URL, you don't need it in the main one as well.

Django Linking a html page to a view

So I know that there are other people who have asked the same question, and I have read through them. However, the solutions provided there are giving me a strange error, and I would appreciate any help in understanding it.
So here's my home.html file:
<head>
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
<!-- Sign Up -->
Sign Up
</body>
And here's my views.py:
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, "home.html")
def signup(request):
return render(request, "signup.html")
And here's my urls.py:
from django.conf.urls import url
from .views import home, signup
urlpatterns = [
url(r'^signup/', signup, name="signup"),
url(r'^', home, name="home"),
]
Thank you for all the help :)
Edit:
The error message is
Reverse for 'signup' not found. 'signup' is not a valid view function or pattern name.
Also I actually changed the way I did urls.py. Now, I only have one urls.py in my main mysite folder:
from django.conf.urls import url, include
from django.contrib import admin
from home import views
from accounts import views as accountsViews
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home),
url(r'^signup', accountsViews.signup),
]
Your second url in 'urls.py' does not have a name.
The url tags would not be able to find them by name -- '{% url 'signup' %}'