Django cannot redict some of urls with uwsgi - django

In project/urls.py which is the root urls.py .
urlpatterns = [
url(r'^webadmin/', include('webadmin.urls')), # With templates and plain text for returning.
url(r'^api/', include('restAPI.urls')), # Without templates. Only plain text for returning.
]
In webadmin/urls.py
urlpatterns = [
url(r'^api/login/', views.login, name='login'),
url(r'^api/search/', views.search, name='search'),
url(r'^api/delProject/', views.delProject, name='delProject'),
url(r'^api/addProject/', views.addProject, name='addProject'),
url(r'^api/statistics/', views.statistics, name='statistics'),
url(r'^(?P<action>(users|login|statistics|projects))/', views.index, name='webadmin'),
url(r'^', views.index, name='index'),
]
All the url in restAPI are working correctly.
But in webadmin, all the url are redict to views.index.
For example, when I goto http://example.com/webadmin/api/login/, it will show http://example.com/webadmin/.
If I remove the last line url(r'^', views.index, name='index'), there is nothing changed.
If I use python3 manage.py runserver, it has rigth result.
My uwsgi ini is:
[uwsgi]
chdir=/var/www/project
wsgi-file=/var/www/project/project/wsgi.py
static-map = /static/=/var/www/project/static/
max-requests=5000
daemonize=/var/log/uwsgi/project.log
http = :8000
vacuum = true
master = true

You need to add end of the line anchor $
url(r'^$', views.index, name='index'),

Related

Django - URL configuration

Here is something i wanna ask if i try this code i can go to login page but my url look like this http://127.0.0.1:8000/%2Flogin/. What is this %2F?
urlpatterns = [
path("", views.index, name="index"),
path("<str:slug>", views.redirect, name='redirect'),
path('/login/', views.logIn, name='login')]
And i remove slash from login url i get an error message
Page not found (404) Request Method:
GET Request URL:
http://127.0.0.1:8000//login/
after removing slashes here is the code
urlpatterns = [
path("", views.index, name="index"),
path("<str:slug>", views.redirect, name='redirect'),
path('login', views.logIn, name='login')]
So, i wanna know is that why are the slashes affecting the url for login but not <str:slug>
Try this:
urlpatterns = [
path("login/", views.logIn, name='login'),
path("<str:slug>/", views.redirect, name='redirect'),
path("", views.index, name="index")
]
The order of the entries matter and always add a trailing /, unless you have root like views.index

why 404 in django url?

I made url for signUp page.
but it returns 404 error.
all of the other urls work well.
I don't know the reason.
main urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('mobileWeb.urls')),
path('api/', include('api.urls')),
]
Application urls
urlpatterns = [
path('', views.index, name='index'),
path('index', views.index, name='index'),
path('addComment', views.addComment, name='addComment'),
# users
path('signUp', views.signUp, name='signUp'),
path('accounts/', include('allauth.urls')),
path('martDetail/<int:martId>', views.martDetail, name='martDetail'),
path('trade/<int:itemId>', views.trade, name='trade'),
path('registerMart', views.registerMart, name='registerMart'),
path('registerItem', views.registerItem, name='registerName'),
path('delete', views.delete, name='delete'),
path('deleteMart', views.deleteMart, name='deleteMart'),
path('deleteItem', views.deleteItem, name='deleteItem'),
path('purchaseItem', views.purchaseItem, name='purchaseItem'),
path('selectItem', views.selectItem, name='selectItem'),
path('addStatistics', views.addStatistics, name='addStatistics'),
path('viewStatistics', views.viewStatistics, name='viewStatistics'),
path('imtPosRegister', views.imtPosRegister, name='imtPosRegister'),
path('imtPosRegisterTest', views.imtPosRegisterTest, name='imtPosRegisterTest'),
path('imtPosSaleInfoTest', views.imtPosSaleInfoTest, name='imtPosSaleInfoTest'),
path('imtPosSaleConfirmTest', views.imtPosSaleConfirmTest, name='imtPosSaleConfirmTest'),
path('fsOe9ms1b', views.fsOe9ms1b, name='fsOe9ms1b'),
path('fsOe9ms1b_ma', views.fsOe9ms1b_ma, name='fsOe9ms1b_ma'),
path('ssOe9ms1b', views.ssOe9ms1b, name='ssOe9ms1b'),
path('ssOe9ms1b_ma', views.ssOe9ms1b_ma, name='ssOe9ms1b_ma'),
path('tsOe9ms1b', views.tsOe9ms1b, name='tsOe9ms1b'),
path('tsOe9ms1b_ma', views.tsOe9ms1b_ma, name='tsOe9ms1b_ma'),
path('writeChatting', views.writeChatting, name='writeChatting'),
path('imageUploadChatting', views.imageUploadChatting, name='imageUploadChatting')
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
404 in web browser
404 in console
The url path matching is case-sensitive. In your paths you wrote:
path('signUp', views.signUp, name='signUp'),
with uppercase U in signUp. I would however advise to use only lowercase, and rewrite this to:
path('signup/', views.signUp, name='signUp'),
The same with other paths in your urls.py.
Try visiting http:\127.0.0.1:8000\signUp

Django: Redirect if the link doesn't match urlpatterns

How do I make django to redirect from www.example.com to www.example.com/home ?
Below you can see my urlpaterns and I'm using url(r'', RedirectView.as_view(pattern_name='home', permanent=False))to redirect to my /home page but when it detects a link without / at the end it redirects me to the /home page.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home, name="home"),
url(r'^surukkam/$',views.shrink,name="surukkam"),
url(r'^(?P<inputURL>[0-9a-zA-Z]+)/$',views.retrieve,name="virivu"),
url(r'', RedirectView.as_view(pattern_name='home', permanent=False))
]
it is redirecting because you missed ^$ at the end of home URL.
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^home/$', views.home, name="home"),
url(r'^surukkam/$',views.shrink,name="surukkam"),
url(r'^(?P<inputURL>[0-9a-zA-Z]+)/$',views.retrieve,name="virivu"),
url(r'^$', RedirectView.as_view(pattern_name='home', permanent=False))
]
^$ - it is a regular expression that specifies the start and end points of a URL string.
whenever your Django detects the empty string it will direct to this URL.

What's the meaning of using patterns in Django URLs?

In my Django project, I find the project urls.py resolve URLs directly
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/', include('test.urls')),
]
but I find the app urls.py solution always use
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
)
when I try to change app's urls.py to
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
]
or
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
urlpatterns += patterns('',
url(r'^new$', views.new, name='new'),
)
also works, so I want to know the meaning of using patterns and which one is better.
Patterns is deprecated since 1.8 (and removed in 1.10)
from the 1.8 docs:
Deprecated since version 1.8:
urlpatterns should be a plain list of django.conf.urls.url() instances instead.

Django only matches urls with an app prefix/identifier

I'm having a problem with django's URL system. I'm using Django 3. I have the following in my project's 'urls.py'
urlpatterns = patterns('',
url(r'^$', include('siteadmin.urls')),
)
and this in the 'urls.py' of a django app in the project, called 'siteadmin':
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
#...trimmed
)
With this setup, the following happens:
http://localhost/ => works perfectly, renders the 'home' view
http://localhost/register/ => breaks. (gives 404 and django's standard "The current URL, register, didn't match any of these.")
However, when I change the project's 'urls.py' to include the following:
urlpatterns = patterns('',
url(r'^app/', include('siteadmin.urls')),
)
And include /app/ in 1. and 2., both urls work perfectly. That is to say:
localhost/app/ => works perfectly
localhost/app/register => works perfectly.
What am I missing? Why does #2 break in the first case, but not the second?
Remove the dollar sign from the regex in the project urls.py:
urlpatterns = patterns('',
url(r'^', include('siteadmin.urls')),
)