I have in my main urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('scoring.urls', namespace='scoring')),
]
and in my app urls:
urlpatterns = [
url(r'scoring/(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
url(r'^scoring/$', views.index, name='index'),
]
and in my template:
<li>Scoring</li>
but what {% url 'scoring:index' %}generates is localhost/instead of localhost/scoring. Why?
At first, you can add the ^scoring prefix in the main urls.py file, instead of writing it everywhere in your scoring urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^scoring/', include('scoring.urls', namespace='scoring')),
]
Then in your scoring urls.py make sure to add app_name:
app_name = 'scoring'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
]
(Note that I removed the scoring prefix in the url patterns.)
Now, as you've added app_name, the reversing in your template should work as expected.
Related
This is my main urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("shop.urls")),
]
I want that any url entered by user will redirect to shop.urls and find there
like if the user enters /index it will search index in shop.urls not in main urls.
My shop.urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('index', views.index),
]
You need to add / at the end of route so:
urlpatterns = [
path('', views.index),
path('index/', views.index),
]
Then enter the requested url as http://127.0.0.1:8000/index/
In main urls:
just give route name.
urlpatterns = [
path('shop/', include("shop.urls")),
]
And this is your shop urls:
urlpatterns = [
path('index/', views.index),
]
After changing above code.
You can navigate like this in your browser:
localhost:8000/shop/index/
You will redirect to index page.
I am getting a duplicate url pattern from newly added app : http://127.0.0.1:8000/quote/quote/new/
base app urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
path('', include('post.urls')),
path('quote/', include('quote.urls')),
]
New app urls.py:
urlpatterns = [
path('quote/new/', QuoteCreateView.as_view(), name='quote-create'),
]
I have tried: path('', include('quote.urls')), in base urls.py but get the same problem.
In your "base app" urls.py you've got this line:
path('quote/', include('quote.urls')),.
In "new app" ("quote app") you've got this line:
path('quote/new/', QuoteCreateView.as_view(), name='quote-create'),
When you include 'quote.urls', each of the URLs is added to the quote/ prefix, which you've set in "base app". In you want your app to create URLs like quote/new, then you should do the following:
in "base app" urls.py:
path('quote/', include('quote.urls')),
in "new app" (or "quote app") urls.py:
path('new/', QuoteCreateView.as_view(), name='quote-create'),
remove the "quote/" in the url.py of your app. you already have a pre-fix in urls.py base app.
urlpatterns = [
path('new/', QuoteCreateView.as_view(), name='quote-create'),
]
base app urls.py:
urlpatterns = [ path('quote/', include('quote.urls')), ]
New app urls.py:
urlpatterns = [ path('new/', QuoteCreateView.as_view(), name='quote-create'), ]
my site used to be ok but since I moved my website from my old server to new one suddenly my inner pages does not appear to work (my main page works ok) and I get 404 error with saying none of url patterns match the link.
I think I should change something due to changing the server but I don't know what.
this is my primary urls.py:
urlpatterns = [
url(r'^',include('BestOfBrands.urls')),
url(r'^admin/', admin.site.urls),
# url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^accounts/', include('allauth.urls')),
url(r'^search/', include('haystack.urls')),
]
and this is my urls.py file which is in "BestOfBrands" Folder (my webapp folder):
urlpatterns= [
url(r'^$', views.index, name='index'),
url(r'^(?P<cg>[-\w]+)-Category/$', views.category, name='category'),
url(r'^(?P<cg>[-\w]+)-Category/(?P<scg>[-\w]+)-SubCategory/(?P<s2cg>[-\w]+)-Brands/$', views.sub2category, name='sub2category'),
url(r'^(?P<cg>[-\w]+)-Category/(?P<scg>[-\w]+)-SubCategory/(?P<s2cg>[-\w]+)-Brands/(?P<br>[-\w]+)/$', views.brand, name='brand'),
url(r'^(?P<sub2category_id>[0-9]+)/vote/$', views.vote, name='vote'),
url(r'^(?P<brand_id>[0-9]+)/comment/$', views.comment, name='comment'),
url(r'^(?P<comment_id>[0-9]+)/replycomment/$', views.replycomment, name='replycomment'),
url(r'^(?P<s2url>[-\w]+)/addbrand/$', views.addbrand, name='addbrand'),
url(r'^(?P<usern>[-\w]+)-Profile/$', views.profile, name='profile'),
url(r'^Change-Avatar/$', views.changeavatar, name='changeavatar'),
url(r'^upload-avatar/$', views.uploadav, name='uploadav'),
url(r'^(?P<comment_id>[0-9]+)/like/$', views.like, name='like'),
url(r'^(?P<rcomment_id>[0-9]+)/likereply/$', views.likereply, name='likereply'),
]
I'm trying to set my django homepage, linked to the root of my website: i.e. http://127.0.0.1:8000/index/ but instead, I keep getting errors where Django is searching for myapp as the homepage: http://127.0.0.1:8000/myapp/. I would just like to land on a homepage with "index" in the url instead of "myapp/"
The error is as follows:
Using the URLconf defined in Main.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
[name='myapp_index']
publications/ [name='publications']
^static\/(?P<path>.*)$
The current path, myapp/, didn't match any of these.
Views.py
def index(request):
return render(request, 'index.html')
Main/urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
myapp/urls
from django.urls import path
from logbook import views
urlpatterns = [
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
path('publications/', views.publications, name='publications'),
]
Now, if I change the path in Main/urls.py to path('myapp/', include('myapp.urls')), I land on the appropriate homepage, except, I would prefer that there is "index/" listed in the URL instead of "myapp/".
I may be missing something very simple here. This is what I think is my MWE, happy to add more.
I was having the same problem and I was cracking my head finding a solution.
The solution was to erase browser's cache and history
I realized that because when I tried with incognito mode worked perfectly!
This should be in the main urls.py
path('', views.index, name='index')
instead of my_app/urls.py
path('myapp/', include('myapp.urls'))
path('index/', include('myapp.urls'))
That might get the desired result.
Did you try following:
Main/urls
...
path(r'^', include('myapp.urls')),
...
myapp/urls
...
path(r'^$', views.index, name='index'),
# path('', views.myapp_index, name='myapp_index'), ## REMOVE THIS ROW ##
...
first you must choose one of below lines in myapp.urls and delete other
you can not set two views for one url patterns
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
you have two solution for get index/ as main url
1.change Main.urls like below
path('', include('myapp.urls')),
to
path('index/', include('myapp.urls')),
2.change myapp.urls like below
path('', views.index, name='index'),
to
path('index/', views.index, name='index'),
you actually changed name of urls that using in python codes not path of urls
edit
Do you mean index/ like the following?
www.example.com/
so u must do that as above but delete "index/"
so u cant do it as below :
change Main.urls like below
path('', include('myapp.urls')),
to
path('^$', include('myapp.urls')),
or
change myapp.urls like below
path('', views.index, name='index'),
to
path('^$', views.index, name='index'),
This is what worked for me (Django2) - pointing to index action in 'static_pages' app's views.py as home page:
main urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('static_pages.urls')), #included urls from static_pages app
]
static_pages/urls.py:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
static_pages/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("First Django2 Message!")
Note that 'static_pages' app is added to INSTALLED_APPS in settings.py.
Now, calling http://127.0.0.1:8000/ I got: "First Django2 Message!".
My urls.py looks like this:
urlpatterns = [
path('',views.index, name='index'),
path('entry/(<int:pk>)' , views.details,name='details'),
path('admin/', admin.site.urls),
]
but when i try to run it i get error as cannot find path.
Attempt 1 :
I tried to use url instead but I am not sure how to use second line into url. This does not seems to work:
urlpatterns = [
url(r'^$',views.index, name='index'),
url(r'^entry/(?P<pk>\d+)/' , views.details,name='details'),
url(r'^admin/', admin.site.urls),
]
I wanted to comment on Monhammand's answer. But, I couldn't do that, because I needed to have at least 50 reputations. So, I submit this as an answer.
If you would like to use regular expressions in Django 2.X, you can use re_path().
https://docs.djangoproject.com/en/2.0/ref/urls/#re-path
urlpatterns = [
re_path(r'^$',views.index, name='index'),
re_path(r'^entry/(?P<pk>\d+)/$' , views.details,name='details'),
re_path(r'^admin/', admin.site.urls),
]
If you are using django 2.x, do like this:
urlpatterns = [
path('',views.index, name='index'),
path('entry/<int:pk>/' , views.details,name='details'),
path('admin/', admin.site.urls),
]
If you are using django 1.x, do like this:
urlpatterns = [
url(r'^$',views.index, name='index'),
url(r'^entry/(?P<pk>\d+)/$' , views.details,name='details'),
url(r'^admin/', admin.site.urls),
]
/ and $ are important