multiple objects to templates with render doesn't work - django

I have a problem I want to send more than one object to the same page on tamplate but happens with me Problem NoReverseMatch at /
def index(request):
boards=Board.objects.all()
categorys=Category.objects.all()
context = {
'boards':boards ,
'categorys':categorys,
}
return render(request,'home.html',context=context)
urlpatterns = [
path('', views.index, name='home'),
path('track/<int:track_id>', views.record_view, name='track1'),
path('boards/<int:id>', views.boards_topic, name='boards_topic'),
]
And Try reversing the order of the last 2 urls and stay problem
urlpatterns = [
path('', views.index, name='home'),
path('', views.category_view, name='home'),
...
]
def index(request):
boards=Board.objects.all()
return render(request,'home.html',{'boards':boards})
def category_view(request):
categorys=Category.objects.all()
return render(request,'home.html',{'categorys':categorys})
What is solution and thank you

Related

Django can't overwrite logout view

I use django.contrib.auth.urls to logout user. I want and I have view logout but I don't know why when I go to accounts/logout/ url django does not use mine, but default view for logout.
from django.urls import path, include
import django.contrib.auth.urls
from . import views
app_name = 'accounts'
urlpatterns = [
path('', views.index, name='index'),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/sign_up/', views.sign_up, name="sign-up"),
path('accounts/logout/', views.logout_view, name="logout"),
]
def logout_view(request):
print(request.user)
context = {'user2': request.user}
logout(request)
return render(request, 'accounts/logged_out.html', context)
Since path('accouts/', …) is ordered first it will fire the logout/ path [GitHub] in the django.contrib.auth.urls module.
If you change the order, it will thus first match with the logout_view and fire that view:
urlpatterns = [
path('', views.index, name='index'),
path('accounts/logout/', views.logout_view, name='logout'),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/sign_up/', views.sign_up, name='sign-up'),
]

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

how to open template with button click

I want to open another template like "booking" in following example by clicking.
projectforms
bookingform
urls
templates
index.html
projectforms
urls
bookingform.urls
urlpatterns = [
path('' , views.purchasing_view, name="purchasing"),
path('',views.add_model, name="booking"),
path('' , views.payment_view, name="payment"),
path('' , views.payment_view, name="payroll"),
]
projectforms.urls
from bookingform.views import add_model,
purchasing_view,payment_view,payroll_view,index_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', index_view),
path('booking/',add_model),
path('purchasing/', purchasing_view),
path('payments/', payment_view),
path('payroll/', payroll_view),
]
How to call "booking" url with following button.
<a class="btn">Booking</a>
It was issue with urlpatterns.
projectforms.urls
urlpatterns = {
path('admin/', admin.site.urls),
path('',include(('bookingform.urls','index'), namespace='index')),
}
bookingform.urls
urlpatterns = [
url(r'^$', views.index_view, name="index"),
url(r'booking', views.add_model, name="booking"),
url(r'purchasing', views.purchasing_view, name="purchasing"),
url(r'payments', views.payment_view, name="payments"),
url(r'payroll', views.payroll_view, name="payroll"),
]

url conflict in Django

I get the same page regardless of what url I use. I get the home.html page for all the ones I have listed. Any suggestions would be valuable. Thanks!
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^signup/', include('log.urls')),
url(r'^login/', include('log.urls')),
url(r'^logout/', include('log.urls')),
url(r'^', include('log.urls')),
]
and from
from django.conf.urls import url
from . import views
# We are adding a URL called /home
urlpatterns = [
url(r'^signup/', views.signup, name='signup'),
url(r'^login/', views.login, name='login'),
url(r'^logout/', views.logout, name = 'logout'),
url(r'^', views.home, name='home'),
]
Your pattern is wrong, for everything you are including log.urls. It should be
project urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('log.urls')),
]
and then,
log urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^signup/$', views.signup, name='signup'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name = 'logout'),
url(r'^$', views.home, name='home'),
]

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.