How to add poll code to the django-cms url.py file? - python

I am new to Django-cms but I couldn't add the poll URL code to the url.py file. I follow every step in the Django-cms documentation but I still couldn't succeed can anyone help me with it.
This the documentation of the Django-cms:
integrating_applications
" Install the application from its GitHub repository using pip:
pip install git+http://git#github.com/divio/django-polls.git#egg=polls
Let’s add this application to our project. Add 'polls' to the end of INSTALLED_APPS in your project’s settings.py (see the note on The INSTALLED_APPS setting about ordering ).
Add the poll URL configuration to urlpatterns in the project’s urls.py:
urlpatterns += i18n_patterns(
re_path(r'^admin/', include(admin.site.urls)),
re_path(r'^polls/', include('polls.urls')),
re_path(r'^', include('cms.urls')),
)
Note that it must be included before the line for the django CMS URLs. django CMS’s URL pattern needs to be last, because it “swallows up” anything that hasn’t already been matched by a previous pattern. "
I had added the "polls" in the setting into the installation app code. But---
My question is where or how will put this code to the urls.py file. -- my urls.py file looks like this:
from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
from django.urls import include, path
admin.autodiscover()
urlpatterns = [path("sitemap.xml", sitemap, {"sitemaps": {"cmspages": CMSSitemap}}),]
urlpatterns += i18n_patterns(
path("admin/", admin.site.urls),
path("", include("cms.urls"))
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += i18n_patterns(
path("admin/", admin.site.urls),
path("polls/", include("polls.urls")),
path("", include("cms.urls"))
)
It will include all the sub-urls that are listed in your other polls/urls.py file under the "mywebsite/polls/" URL. This is the standard Django way to organize your routes: per app.

Related

How to solve urls not found issue in django

I am building an app using django and notice that some new urls that i have created, the list is provided below are not being found.
I read somewhere that the slug might be the reason django cannot found the urls but i am not sure
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(config("ADMIN_URL"), admin.site.urls),
path("", include("core.urls", namespace="core")),
]
application urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
app_name = 'core'
urlpatterns = [
path('index/', views.index, name='index'),
path('home/', views.home, name='home'),
path('home/<slug:slug>/', views.user_lessons_details, name='user-lessons-details'),
path('home/join-transaction/', views.user_lessons_join_transaction, name='user-lessons-join-transaction'),
path('home/start-transaction/', views.new_test_epoint, name='new-test-epoint'),
path('home/draft/<slug:slug>/', views.user_lessons_core_action, name='user-lessons-draft'),
path('teachers/lessons/create/', views.merchant_lessons_create, name='teachers-lessons-create'),
path('teachers/lessons/list/', views.teachers_lessons_list, name='teachers-lessons-list'),
path('teachers/lessons/<slug:slug>/', views.teachers_lessons_details, name='teachers-lessons-details'),
path('teachers/lessons/<slug:slug>/update/', views.teachers_lessons_update, name='teachers-lessons-update'),
path('teachers/lessons/<slug:slug>/delete/', views.teachers_lessons_delete, name='teachers-lessons-delete'),
path('teachers/drafts/create/', views.teachers_core_actions_create, name='teachers-drafts-create'),
path('teachers/drafts/list/', views.teachers_core_actions_list, name='teachers-drafts-list'),
path('teachers/drafts/<slug:slug>/', views.teachers_core_actions_details, name='teachers-drafts-details'),
path('teachers/drafts/<slug:slug>/update/', views.teachers_core_actions_update, name='teachers-drafts-update'),
path('teachers/drafts/<slug:slug>/delete/', views.teachers_core_actions_delete, name='teachers-drafts-delete'),
path('teachers/home/list/', views.teachers_home_list, name='teachers-home-list'),
path('teachers/home/<slug:slug>/', views.teachers_home_details, name='teachers-home-details'),
path('teachers/home/create/course/', views.teachers_home_transaction_quote, name='teachers-home-create-course'),
path('teachers/home/create/something/', views.teachers_home_create_transaction, name='teachers-home-create-something'),
path('teachers/home/create/fields-selection/', views.teachers_home_fields_selection, name='teachers-home-fields-selection'),
# ALL THE NEW URLS BELOW return NOT FOUND in django no matter what i do
path('teachers/home/account/settings/', views.teachers_settings, name='teachers-settings'),
path('teachers/account/settings/update-password/', views.teachers_settings_update_password, name='teachers-settings-update-password'),
path('teachers/account/payments/', views.teachers_payments, name='teachers-payments'),
path('teachers/account/payments/update/', views.teachers_payments_update, name='teachers-payments-update'),
path('teachers/account/analytics', views.teachers_analytics_stuff, name='teachers-analytics-stuff')
]
How can i possibly solve this urls not found issue in django?
Please dont mind the urls meaning

Django URL patterns not being read

So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))

(urls.W005) URL namespace 'LnkIn' isn't unique.

Hi I'm getting this error when doing my migrations or using the python manage.py runserver command.
(urls.W005) URL namespace 'LnkIn' isn't unique.You may not be able to reverse all URLs in this namespace.
This is how I have my urls.py inside my app directory (LnkIn).
from django.conf.urls import url
from . import views
app_name = 'LnkdIn'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
url(r'^login_user/$', views.login_user, name='login_user'),
url(r'^logout_user/$', views.logout_user, name='logout_user'),
url(r'^(?P<user_id>[0-9]+)/$', views.profile, name='profile'),
url(r'^(?P<song_id>[0-9]+)/favorite/$', views.favorite, name='favorite'),
url(r'^trabajos/$', views.trabajos, name='trabajos'),
url(r'^crear_oferta/$', views.crear_oferta, name='crear_oferta'),
url(r'^(?P<user_id>[0-9]+)/create_trabajo/$', views.create_trabajo, name='create_trabajo'),
url(r'^(?P<user_id>[0-9]+)/crear_amistad/$', views.crear_amistad, name='crear_amistad'),
url(r'^(?P<user_id>[0-9]+)/delete_trabajo/(?P<trabajo_id>[0-9]+)/$', views.delete_trabajo, name='delete_trabajo'),
url(r'^(?P<album_id>[0-9]+)/favorite_album/$', views.favorite_album, name='favorite_album'),
url(r'^(?P<album_id>[0-9]+)/delete_album/$', views.delete_album, name='delete_album'),
]
And this is how I have my urls.py in my main directory.
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^LnkdIn/', include('LnkdIn.urls')),
url(r'^', include('LnkdIn.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I really have no idea what could I've done wrong. I checked in my views and in my templates and everything seems to be fine, I don't seem to be having any typo on my urls. I've search but haven't find this error, I've seem similars one and they suggest to check not having mistakes in the urls.
I'm using Python 2.7 and Django 1.10.
You are importing LnkdIn.urls twice to your application urlpatterns.
You should only do it once, so choose either one from the section below
url(r'^LnkdIn/', include('LnkdIn.urls')),
or
url(r'^', include('LnkdIn.urls')),

Django, url not found?

I'm trying out Python Django. With eclipse pyDev. But I'm unable to simply get my first url to display.
This urls.py is from the Cr package.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
This urlspy is from the Crowd package.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'Crowd/', include('Cr.urls'))
]
So what I've understood from this the Crowd package is the "main" webservice(?), and by using include I can whenever the regular expression matches Crowd, will pass it on to the other urls.py(Cr). But the debugger passes:
Using the URLconf defined in Crowd.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, crowd, didn't match any of these.
my views.py file
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
I tried to access it with http://127.0.0.1:8000/Crowd
Below is an image of the project folder.
Can we see your settings.py file? There is a place in there where you define your project's url file. I'm assuming it's right now either not there or it's pointing to the wrong place, because Django can't find your urls.py file.
For example, in my settings.py file for one of my projects, I have:
ROOT_URLCONF = 'Freya.urls'
Where "Freya" is my project name
Just for reference, not that I know this will solve your problem, this is what (part of) my urls.py file looks like for one of my projects:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'Crowd/', include('Cr.urls'))
]
just use this urls.py file

Use dynamic alias in Python Django when switching from Debug to Production

I have the following http address when running in development (manage.py runserver)
http://127.0.0.1:8000/
When using wamp i use an alias named picon
http://localhost/picon/
When clicking on links from the nav bar the navigation works fine for both deployments. But when i click from the following views.py the urls break;
def addcustomer(request):
form = AddCustomerForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/picon/customers')
return render_to_response("addcustomer.html",
locals(),
context_instance=RequestContext(request))
In the production server i use the prefix /picon/ because otherwise the link can not be found when using an alias. Off course this alias is not available on the development version.
So my question, can i create a dynamic alias that has a relation to the debug status in the settings.py file.
for example;
If Debug:
alias = ''
else:
alias = 'picon/'
if this is possible, how would i then reference to the alias in the views.py using the following line;
return HttpResponseRedirect('/picon/customers')
Edit: added my urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'signups.views.home', name='home'),
url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),
url(r'^customers/$', 'formlist.views.customers', name='customers'),
url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),
#url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Any suggestions are much appreciated.
Regards.
This is exactly why you should not hard-code URLs in views or templates. Use django.core.urlresolvers.reverse - or the {% url %} template tag - to calculate the URL dynamically, including any prefix.
return HttpResponseRedirect(reverse('customers'))
where 'customers' is the name value from the URLconf.
You can include the urls from another file, dynamically:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
#PAY ATTENTION HERE
from another_location import urls as another_urls
admin.autodiscover()
if settings.DEBUG:
urlpatterns = url(r'^picon/', include(another_urls))
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
else:
urlpatterns = url(r'^', include(another_urls))
Having your another_location.urls file like this:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'signups.views.home', name='home'),
url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),
url(r'^customers/$', 'formlist.views.customers', name='customers'),
url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),
#url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
url(r'^admin/', include(admin.site.urls)),
)
You must ensure such file is pythonpath-reachable via imports.
Edit: also, don't hardcode URLS. Always use reverse. It will be harmless to you since you gave names for each of your urls (reverse('customers') will give you the right url no matter where is it deployed).

Categories