Writing my first Django app - python

I am trying my hand at learning Django and trying out the step-by-step tutorial at https://docs.djangoproject.com/en/2.0/intro/tutorial03/.
I have completed the app (well, till the Part 7) and is working as expected (and has been explained in the tutorial).
The only problem (so far) I am facing is when I am trying to navigate from the "Admin" page to the linked page "VIEW SITE" when I am being presented with "Page not found (404)" error. An image is being attached to make the situation clearer.
The link is pointing to "http://127.0.0.1:8000/" whereas it should be pointing to "http://127.0.0.1:8000/polls/". When I add the missing part of the path (manually) in the address bar the correct page (as expected) is presented.
I have tried to search on this as well as many other forums but could not get the right solution.
I am using Django 2.0.6 and Python 3.6.4 on mac sierra.
Shall be grateful for a lead on this.
Thanks
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
polls/template/polls/index.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Error on navigation at VIEW SITE

you should open http://127.0.0.1:8000/polls/
not
http://127.0.0.1:8000/.
If you wanna use http://127.0.0.1:8000/ then your path should be
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]

It should be like this:
path('', include('polls.urls')),
not like this:
path('polls/', include('polls.urls'))
Because it should be the root url of your website

Here is what I have done (may be not the most elegant solution but works just fine).
I have modified the "mysite/urls.py" file as shown:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')),
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
This way I am able to access the "polls" page from both "View Site" link on the Django Admin page (url: "127.0.0.1:8000") as well as from link residing elsewhere (url: "127.0.0.1:8000/polls/").
Thanks for all the help.
PS. Visiting https://docs.djangoproject.com/en/2.0/topics/http/urls/ may be of help to learners like me.

Related

Django url pattern is not a registered view function or pattern

I have a small Django web app, with multiple applications inside them. I have used the include in the urls.py files, but whenever I reference the URLs in the HTML files they don't load. Below are my 3 urls.py files. The one I'm having an issue will specifically is the nodes url pattern in the nodes urls.py
#main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
]
#account urls.py
from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('dashboard/', views.dashboard, name='dashboard'),
path('nodes/', include('nodes.urls')),
]
#nodes urls.py
from django.urls import path
from . import views
app_name = 'nodes'
urlpatterns = [
path('', views.nodes, name='nodes'),
]
This is my HTML file where I am referencing the URL pattern:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'nodes' %}">Nodes</a>
</li>
Due to this pattern name you set in urls file if change HTML file to this it must be fixed:
<li {% if section == 'nodes' %}class="active"{% endif %}>
<a class="nav-link" href="{% url 'account:nodes:nodes' %}">Nodes</a>
</li>
just for tips :) if import urls file at the django shell and print list of urlspatterns you could see default name assign to your nodes path.

Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name

I am getting the below error when I am trying to load the home page:
Reverse for 'display_data' not found. 'display_data' is not a valid view function or pattern name
My views.py file is as follows:
def home(request):
#query_results = QRC_DB.objects.all()
return render(request, 'display_data.html')
def display_data(request,component):
#query_results = QRC_DB.objects.all()
return HttpResponse("You're looking at the component %s." % component)
My urls.py file under the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
]
The urls.py file under the project is as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
And my html file (display_data) code is as follows :
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}
Can anyone please help me to find out the mistake ?
Thanks.
Your urls.py file doesn't contain any url for display_data.
When you're trying to click the link rendered in the HTML tag, namely,
<li>SQL</li>
it tries to resolve the URL display_data.
First, it checks the root urls.py file. Among the following:
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
it matches the second one. Then it loads the fusioncharts.urls but the fusioncharts.urls doesn't contain any URL for display_data. That's why you are getting the error.
The urls.py file should be like this:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:arg>', views.display_data, name='display_data'),
]
# There is a change in urls.py and in your template 'display_data.html'
urls.py
urlpatterns = [
path('home/', views.home, name=''),
path('display_data/<str:component>', views.display_data, name='display_data'),
]
display_data.html
{% block content %}
<h3>Display the test results</h3>
<div id="container" style="width: 75%;">
<canvas id="display-data"></canvas>
<li>SQL</li>
</div>
{% endblock %}

Cannot Get image From Database Django Framework

I want to create a setting in admin panel which will help me to change the website logo from the admin panel. I create a model for database and it's working and uploading an image to the database + folder. but I cannot get this image to the Website template. when I'm viewing page source image SCR is empty.
my Model.py
class WebLogo(models.Model):
Logotitle = models.CharField(max_length=50,unique=False)
SiteLogo = models.ImageField(upload_to='webimages')
def __str__(self):
return self.Logotitle
my Views.py
from .models import Post,WebLogo
def Display_WebLogo(request):
# getting all the objects of hotel.
WebsiteLogo = WebLogo.objects.all()
return render((request, 'index.html',{'web_images' : WebsiteLogo}))
my project Urls.py
urlpatterns = [
path('', views.PostList.as_view(), name='home'),
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my Html Code
<a href="index.html"><img src="{{ WebLogo.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
my app url
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
In HTML Code, use 'web_images' not WebLogo like this
<a href="index.html"><img src="{{ web_images.0.SiteLogo.url }}" class="img-fluid" alt="logo">
</a>
It looks like {{ data.0 }}. See Variables and lookups.
You should write the required information in the settings.py file.
https://docs.djangoproject.com/en/dev/ref/settings/#media-root

I get page not found (404) error when going to "http://127.0.0.1:8000/restaurant/sign-in/" page

When I go to "http://127.0.0.1:8000/restaurant/sign-in/" I get page not found (404) error. But I can go to "http://127.0.0.1:8000/restaurant/$" to access the home page.
I also tried "http://127.0.0.1:8000/restaurant/sign-in/$" but this also gives me error (init() takes 1 positional argument but 2 were given).
My urls.py is
from django.contrib import admin
from django.urls import path
from foodtaskerapp import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('restaurant/sign-in/$', auth_views.LoginView,
{'template_name': 'restaurant/sign_in.html'},
name='restaurant-sign-in'),
path('restaurant/sign-out', auth_views.LogoutView,
{'next_page': '/'},
name='restaurant-sign-out'),
path('restaurant/$', views.restaurant_home, name='restaurant-
home'),
]
And my views.py is
from django.shortcuts import render, redirect
def home(request):
return redirect(restaurant_home)
def restaurant_home(request):
return render(request, 'restaurant/home.html', {})
here is the screenshot of the error
I also have
<body>
<form method="POST">
{% csrf_token %}
{{ form }}
<button type="submit">Sign In</button>
</form>
</body>
in sign_in.html but the form don't show up, only Sign In is shown.
only sign is shown but not the form
the syntax for the function you are using has changed in the latest version of django, which is why you got that error (the code you are sharing is in a tutorial made by code4startups which uses an older version of django).
You should modify your path command to:
path('restaurant/sign-in/', auth_views.LoginView.as_view(
template_name='restaurant/sign_in.html'),
name='restaurant-sign-in'),
You have no corresponding function in views.py:
auth_views.LoginView
Also, i guess, you neither have 'restaurant/sign_in.html', so it doesn't redirect to the page.
Add this in views.py:
def restaurant_signIn(request):
return render(request, 'restaurant/sign_in.html')
And corresponding HTML page name: 'sign_in.html',in restaurant directory:
<p>SigninWorks</p>
Your urls.py must look like:
from django.contrib import admin
from django.urls import path
from foodtaskerapp import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('restaurant/sign-in/', views.restaurant_signIn,
name='restaurant-sign-in'),
path('restaurant/$', views.restaurant_home, name='restaurant-
home'),
]

how to format a a url to return something like this "product/4/improved-led-lamp" in the template

I'm trying to return a formatted url that will look like this "edit-book/4/improved-led-lamp" using the {% url %} tag
I've tried {% url 'dashboard:editbook' 4 slug %}
here is the root url.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('home.urls')),
path('dashboard/', include('dashboard.urls')),
path('admin/', admin.site.urls),
]
here is my dashboard app's urls,py
from django.urls import path
from . import views
app_name = 'dashboard'
urlpatterns = [
path('edit-book/<int:id>/<slug:title>', views.BookView.as_view(), name='editbook'),
]
if I visit "edit-book/4/improved-led-lamp" in my browser, it resolves, but when I try to reproduce this in the view using {% url %} tag, it throws noReverseMatch error
this is the error
screenshot of error
Your syntax looks correct and if slug variable consists of slug ([-a-zA-Z0-9_]+) it should be working.
Look into contents of slug. Error screenshot shows that it's an empty string.

Categories