I've already searched for my topic but the results were showed fixes for e.g. 127.0.0.1:8000/polls, 127.0.0.1:8000/app_xy etc. but nor for 127.0.0.1:8000 direct.
I want my Homepage show view-result e. g. "This is my homepage" if I call it with 127.0.0.1:8000
My idea is to edit my website's main-urls.py (myWebsite/urls.py) with the following line:
urlpatterns = [
url(r'^$', <no idea what to do here>),
url(r'^app2/', include('app2.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
I created a view (myWebsite/views.py) which contains:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello from the Homepage')
I had no problems with the views of the apps (app2, polls) and they work fine, but I don't know how to call the view above from the main-urls.py. Or what do you advise me to do?
I hope I explained my problem well enough.
Thx for your help.
As I'm a little advanced now, I am going to answer my question on my own:
The solution is to add the following to make an import of the views and put an URL to the project's urls.py:
from first_app import views
and add the urlpatterns:
urlpatterns = [
... ,
url(r'^$', views.index, name='index')
]
If you go to your browser and connect to http://127.0.0.1:8000/ you will see the result:
Hello from the Homepage
This is a simple respond of the index-function in the views.py. Instead of this you better create a template (e. g. home.html) and render this in the index-function.
Related
I am very new to django and beginning to understand some of the framework however view-route binding is confusing me
There is a persistent issue that when I try to visit any url except for the homepage and /admin I receive a 404, including routes I have declared in my project's urls.py file
also i am following this mdn tutorial
project urls.py
"""trends URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
app named 'articles' urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
app named 'articles' views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the articles index.")
and here is the 404 page I receive
I know this is becoming very long but there is one more odd thing, when I refresh the 404 page, it will toggle between showing me the above screenshot and sometimes show me an old route which is no longer in the urls.py like this
this is on an nginx server with gunicorn, and restarting the nginx service does not solve the issue
In your projects urls.py you have defined
path('articles/', include('articles.urls')),
So by going to YOUR_URL/articles will not give a valid response. Instead try going to YOUR_URL/articles/ or change your path to
path('articles', include('articles.urls')),
Stumbled upon this SO post which lead me to the idea to restart gunicorn and that solved my problem so try running
sudo service gunicorn restart
should fix your problems
No matter what I try in vs code the request is always greyed out. I'm following the Django tutorial and I get a 404 when loading the server because the index function can't be called cause the parameter is greyed, This works perfectly fine in Pycharm, but not vs code. Any solutions?
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
this is the urls for polls
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
And urls for the whole website
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
The grayed out 'request' parameter in your function does not mean that it isn't called, it's telling you that the parameter isn't used, also make sure you use indentation in your function views:
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
This is probably pretty simple but I can't get my head around it. I'm learning Django, have v3.0.4 installed and can't get the URLs from an app to work correctly.
On the project urls.py I have the following:
Project\urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from AppTwo import views
urlpatterns = [
path('', views.index, name='index'),
path('', include('AppTwo.urls')),
path('admin/', admin.site.urls),
]
I've created an app named "AppTwo" and have the following urls.py and views.py in the app:
AppTwo\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('/help', views.help, name='help'),
]
AppTwo\views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<em>My Second App</em>")
def help(request):
return HttpResponse("<em>Help Page!!!</em>")
If I browse to http://127.0.0.1:8000/ the index page loads and I see the text "My Second App" as expected. However if I browse to http://127.0.0.1:8000/help I get page not found 404 error.
I can also browse to the admin page just fine. So far this is a stock project, the only other change I made after creating it was to the settings.py file to install the "AppTwo" application. Based on the documentation, this looks like it should work, so what am I doing wrong?
yep, knew it was simple.
Changed
path('/help', views.help, name='help'),
to:
path('help/', views.help, name='help'),
all good now.
I'm working on django app and i'm facing a problem when i need to navigate from my index.html page to another about.html page. I'm setting everything in this way below:
urls.py (myproject)
from django.urls import path, include
urlpatterns = [
path('', include('weather_app.urls')),
path('about/', include('weather_app.urls'))
]
urls.py (myapp)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name="about")
]
In index.html, everything is working well, so i will not put the code.
and in view.py:
from django.shortcuts import render
import requests
def about(request):
return render(request, "about.html")
i have the code below in my index.html:
About
and i cannot get about.html page when click About link as you can see above.
Can anybody help me?
Thanks in advance..
You have to use this tag to avoid manual rendering of urls.
In your code:
About
Let me know if this works!
Was hoping someone could point me in the right direction with this. I've tried nearly everything I can think of, but I can't seem to get it to work. I've got a set of URLs I'd like to match in Django:
www.something.com/django/tabs/
www.something.com/django/tabs/?id=1
Basically, I want to make it so that when you just visit www.something.com/django/tabs/ it takes you to a splash page where you can browse through stuff. When you visit the second URL however, it takes you to a specific page which you can browse to from the first URL. This page is rendered based on an object in the database, which is why the id number is there. I've tried to account for this in the URL regex, but nothing I try seems to work. They all just take me to the main page.
This is what I have in urls.py within the main site folder:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', include("tabs.urls")),
]
and within urls.py in the app's folder:
urlpatterns = [
url(r'\?id=\d+$', tab),
url(r'^$', alltabs)
]
Would anyone be so kind as to point me in the right direction? Thanks in advance!
You are not following the right approach here. Query paramers are used to change the behaviour of the page slightly. Like a added filter, search query etc.
What i would suggest is you have only one view and render different templates based on query parameters in the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', alltabs),
]
In your alltab views you can have something like this.
def alltabs(request):
if request.GET.get("id"):
id = request.GET.get("id")
your_object = MyModel.objects.get(id=id)
return render_to_response("tab.html", {"object":your_object})
return render_to_response("alltab.html")
Hope this helps
This is not the preferred 'django way' of defining urls patterns, I would say:-)
In the spirit of django would be something like
www.something.com/django/tabs/
www.something.com/django/tabs/1/
....
www.something.com/django/tabs/4/
and for this you define your url patterns within the app for example this way
tabs/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /tabs/
url(r'^$', views.index, name='index'),
# ex: /tabs/5/
url(r'^(?P<tab_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /tabs/5/results/
url(r'^(?P<tab_id>[0-9]+)/results/$', views.results, name='results'),
]
and something similar in your views
tabs/views.py:
from django.shortcuts import get_object_or_404, render
from tabs.models import Tab
def index(request):
return render(request, 'tabs/index.html')
def detail(request, tab_id):
tab = get_object_or_404(Tab, pk=tab_id)
return render(request, 'tabs/detail.html', {'tab': tab})
...
You can follow this django tutorial for more details: