How to djangocms-link to a app-hook subpage? - python

I have an app that provides a form to create and update "Prediction"s.
urls.py:
urlpatterns = patterns('',
url(r'^create/$', PredictionCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)/$', PredictionDetailView.as_view(), name='detail'),
url(r'^$', PredictionListView.as_view(), name='list'),
)
The app is attached with an app-hook to Django CMS 3.0.12.
cms_app.py:
class Predictionhook(CMSApp):
name = "Predictionhook"
urls = ["prediction.urls"]
app_name = "prediction"
apphook_pool.register(Predictionhook)
Is there an easy way to include the 'create' URL of the attached app in the list of possible choices of pages in the djangocms-link plugin?

The standard setup won't do it as far as I'm aware.
I'd create your own copy of djangocms-link and extend it to give a field to take the same thing you'd put in a template URL tag. Then just set up the template to render that field value as a standard Django URL template tag.
So in your new field of the link app you'd add prediction:create and then in the template being rendered you'd have {% url link.myurllinkfield %}

Related

how to read html file on Django

I started make a website for my school project, i use Django Frameworks
i want to make my homepage website, i already have html file, then what should i do to make Django read my homepage
enter image description here
this is my urls.py on main folder
urlpatterns = [
path('admin/', admin.site.urls),
path('katalog/', include('katalog.urls')),
path('', ) #what should i write here?
]
django doc: https://docs.djangoproject.com/en/3.0/topics/http/urls/#example, it offers the below example
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
You have to create in your apps views.py file a FBV or CBV, the easiest is a FBV like below:
def someview(request):
context={}#if you want to add objects to the front end
return render(request, 'yourhtmlfile', context)
and then import it in your urls.py:
from . import views
urlpatterns = [
path('home/', views.someview),

Django: adding simple button to admin generic view

Django newbie here working on a pre-existing app I've inherited. I need to add a cancel/back button to the show (DetailView in Django terms, I think?) page. I know how to do this with anchor tags in HTML. However, I can't figure out where the HTML template is for this view! I believe it's being procedurally generated as a "generic view" or "admin" from this code in urls.py:
from django.conf.urls import url
from . import views
app_name = 'reports'
urlpatterns = [
# ex: /reports/
url(r'^$', views.IndexView.as_view(), name='index'),
# ex: /reports/5/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# ex: /reports/5/results/
url(r'^(?P<pk>[0-9]+)/results$', views.ResultsView.as_view(), name='results'),
# ex: /reports/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote$', views.vote, name='vote'),
]
The bottom bar of the reports show / DetailView page contains the following values when accessed via browser:
I'd like to add a "Cancel" button to this bar, but I can't find where I could add such a button -- I can't even find these existing buttons in the codebase.
An admin/reports_admin.py exists with some configuration that may be relevant, but I don't see an obvious place in that file where I could add this "Cancel" button.
Please help!
I think this is what you want
Overriding admin templates | Django docs
as well you can override admin views, which is also described here, to add additinal functionality.

Django url not finding a template using CreateView in Class-Based Views

I have created a page where a user can add a new item (notes in this case) and I am making use of CBV which I have recently started learning.
This is my model form
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('title', 'note', 'tags')
This is the view in views.py
class NoteCreate(CreateView):
model = Note
form_class = NoteForm
template_name = "add_note.html"
Then this is the url as I used in the urls.py of the app
from django.conf.urls import patterns, url
from . import views
from madNotes.views import NoteCreate, NoteIndex,
urlpatterns = patterns(
'',
url(r'^notes/add/$', NoteCreate.as_view(), name="new_note"),
url(r'^$', NoteIndex.as_view()),
url(r'^(?P<slug>\S+)/$', views.NoteDetail.as_view(), name="entry_detail"),
)
NB: I used the same url as the main page at 127.0.0.1:8000 in the projects urls.py file and it worked.
I have seen several tutorials and even the docs and can't seem to find what I am doing wrong. Will I also need to add a function in order for it to be saved in the db or the CBV will do it all?
EDit: The error I get is this
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/notes/add/
Here is the project's urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from MadNotez import settings
from registration.backends.default.views import RegistrationView
from madNotes.forms import ExRegistrationForm
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
url(r'accounts/register/$', RegistrationView.as_view(form_class = ExRegistrationForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', include(admin.site.urls)),
url('^markdown/', include('django_markdown.urls')),
url('^notes/', include('madNotes.urls')),
#url(r'^$', views.NoteCreate.as_view(), name="new note"), when I used it here it worked
)
you say that is the urls.py of the app, which means it is included by the project's urls.py.
As you show now, all the app's URIs go under the notes prefix:
url('^notes/', include('madNotes.urls')),
so as things stand at present the correct URI for the page is
http://127.0.0.1:8000/notes/notes/add/
In order to clean things up a bit, I'd suggest to modify the app's urls.py to
url(r'^add/$', NoteCreate.as_view(), name="new_note"),
so that the page can be reached at
http://127.0.0.1:8000/notes/add/
This way all the app's pages/services are available under the notes prefix and with a simple name that is consistent with their action (i.e. add, delete, etc)

django project url error

I created a project and created an application orders.
Views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'orders/index.htm')
def orders(request):
return render(request, 'orders/order.html')
urls.py:
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index.htm/', views.index, name='dashboard'),
url(r'^order.html/', views.orders, name='orders'),)
I used a ready to use site template.the link for 2 pages are:
index.htm index.htm is Dashboard
order.html order.html is Orders
when i go to link localhost:8000/orders/ , I click on orders the link looks like this.
and further click on two links in left pane multiple times, the resulting url look like this:
localhost:8000/orders/order.html/order.html
localhost:8000/orders/order.html/index.htm
I want to remove the order.html from middle so that it looks like:
localhost:8000/orders/index.htm
localhost:8000/orders/order.html
The links in your template do not have a leading slash, which means they are relative to the current directory.
If you are on the page with the URL http://localhost:8000/index.html/ and click on a hyperlink with href="order.html", it will take you to
http://localhost:8000/index.html/order.html
To fix it you must either
Add a slash to the links in your template, e.g. <a href="/order.html">
or drop the trailing slash from your URL definition in urls.py, e.g.
url(r'^index.htm', views.index, name='dashboard')
I suggest you use the template tag url in your templates instead of hard coding links.
If possible, I would also remove the .htm extension from both templates and urls.py
Change the following in urls.py file
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index/$', views.index, name='dashboard'),
url(r'^order/$', views.orders, name='orders'),)

Can One register namespace in django without adding a URL pattern?

When creating an apphook for the photologue app into django-CMS, I ran into trouble:
"photologue" is not a registered namespace
when trying to reverse an URL such as:
href="{% url 'photologue:pl-gallery-archive-year' date.year %}"
My current Solution is to add a 'foo' url pattern below the cms urls in urls.py so that foo will not ever be matched, but the photologue namespace is registered.
urls.py:
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps':
{
'cmspages': CMSSitemap,
'photologue_galleries': GallerySitemap,
'photologue_photos': PhotoSitemap,
}}),
url(r'^', include('cms.urls')),
url(r'^foo/', include('photologue.urls', namespace='photologue')),
)
cms_app.py:
class GalleriesApphook(CMSApp):
name = _("Galleries Apphook")
urls = ["photologue.urls"]
app_name = "photologue"
apphook_pool.register(GalleriesApphook)
My question is: Is there a cleaner way to do this ? Like a "register_namespace" function in the django core ?
EDIT
I found something "slightly cleaner" in the imagestore doc. In order not to pollute urls they re-include the cms ones with the namespace.
url(r'^', include('cms.urls', namespace='imagestore'))
I had exactly the same problem. I solved it with #stefanfoulis hint to add application instance name. I did not have app_name when starting this at first time.

Categories

Resources