I couldn't find this info in the Django docs, but I'm sure it is there, I'm just very new and don't know what terms/etc to search on.
How are Django page templates assigned to each page?
I have a login to a Django site, and also SFTP access to the site. I don't think my Django login is a superuser/full-admin though because the interface seems pretty limited compared to other CMS systems. I can edit pages, posts and the media library, but I don't see anything that says how each page is assigned a template.
For example, I have this file /mysite/templates/pages/index.html
I know that template is being used for the home page because it has all of the content that is specific to the home page on it, and changes I make show up on the home page.
I tried copying that file to test.html, but when I browse to test.html in my browser, I get a 404 error (I also get that error if I go to index.html). So there must be something else that maps a template to a page, but I'll be dambed if I can find it. Will I need more access to the admin area, or can I do something with SFTP? I also have SSH access but wasn't able to follow any of the steps online to create a new superuser account for me, for Django.
Edit: Thanks for both answers, after I work through this I'll accept whichever helped the most. I do not have a views.py file, but I think it might be using an extra module for this routing, I have this in my urls.py file:
urlpatterns = patterns("",
("^admin/", include(admin.site.urls)),
url("^$", "mezzanine.pages.views.page", {"slug": "/"}, name="home"),
("^", include("mezzanine.urls")),
)
Is this "mezzanine" something different which changes the answer (location of views.py or list of views)?
url.py is the file that maps the urls to methods that return rendered templates. In essence you define the url and a method and when someone goes to that url, that method gets called which returns a HTTP response with the rendered template. This map is called urlpatterns. In the following example when someone goes to yourwebsite/blog then in the blog apps, view.py, page method is called, which will use a template and render that with specific information.
urlpatterns = patterns('',
url(r'^blog/$', 'blog.views.page'),
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
)
Have a look at this link.
https://docs.djangoproject.com/en/dev/topics/http/urls/
Django uses urls.py files to map paths to views. This match is resolved using regular expressions. When a match is found, Django executes the associated view (usually inside views.py). The view is in charge to render the template required for the path (by finding it on the server's hard disk and loading it).
All aforementioned means that there's no direct association between a url path (i.e www.example.com/path/to/page) and a file on the server's hard disk (i.e /server/path/to/page). It's all performed dynamically by Django's engine when a request comes in.
If you want to know which view is gonna be generated for a specific path, follow the regexs at urls.py until you find the path you're looking for. Then open the view for that url and see inside which template it is rendering.
Reading doc's URL Dispatcher is a good point to start learning about this.
Hope this helps!
Related
I have a Django app that will serve diferent websites.
Each one will have it´s own domain
Each one will have it´s own sub app with templates and views
They all share the same backend, models and data
My aproach
As I already have the database with the segmentation info I need to show the desired products in each site and I will have different views for each sub app, I don´t need to add another field in the models.
I think that it would be easier just to detect the request domain in my main app urls.py and rout home url to the desired sub app.
Something like:
# urls.py
if "name1" in request.domain:
urlpatterns += [path('', include('app1.urls'))]
if "name2" in request.domain:
urlpatterns += [path('', include('app2.urls'))]
else:
urlpatterns += [path('', include('app3.urls'))]
Maybe I should make a middleware and set a global variable I can access in urls.py? Can I use this kind of if statements in urls.py?
Django sites
I checked the Django Sites Framework, but if I understand ir well, it´s more focused in segmenting the database in the models, not the templates and views. In any case I don´t really catch how the Sites framework detects the incomming URL and roots the request to each sub app.
Other intents
I searched for more info and this article can brief the different articles about the issue I found.
https://medium.com/crowdbotics/how-to-use-dynamic-subdomains-in-django-dc1cb2cac00b
But still I don´t get how can I achieve what I need. Sounds very logical just to use my aproach and don´t mess with my database. If it´s possible.
Any clues welcome. Thanks in advance!
I have a django project with specific urls, setup by a 'catchall' URL.
This is so I can go to mysite/living, and have it pass living as a parameter and pull up the appropriate details from my db.
My urls.py:
url(r'^$', views.index, name='index'),
url('about/', views.about_view, name='about_view'),
url('contact/', views.contact_view, name='contact_view'),
url('(?P<colcat>[\w\-]+)/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('(?P<colcat>[\w\-]+)/$', views.collection_view, name='collection_view'),
I am running into the problem where, anything can be passed as a parameter. This is particularly notable with search engines, where mysite/index.html/index.html returns a valid page.
Is there a way to limit the urls that are 'allowed' to be matched?
It is very unlikely for a user to enter/modify URLs manually while browsing. Everyone just googles and clicks whatever link is shown by the search engine. So, You just need to restrict what the search engine indexes.
This can be done by adding a sitemap.xml file to the root of your website.
sitemap.xml specifies all the urls of your website along with some additional information inorder to make it easier for search engines to crawl. If you don't add a sitemap.xml, search engines try to crawl through every possible url. If added they wont.
There is already a sitemap generating framework provided by django: https://docs.djangoproject.com/en/2.1/ref/contrib/sitemaps/
After playing with python-socketios django_example, and seeing that it worked great, I created a new django project, configured it just like the example, copied over the example app to the project (complete with it's overriding of the runserver management command). Everything worked fine, and I was able to make my a few changes so that you can set a nick, some redis stuff to lookup up the sid for a nick, and was able to support sending private messages to a nick. Everything was still working great.
I figured the next logical step was to, rather than having to manually set a nick, require the user to login, expose their username as a var in a script block in the template (I moved scripts/index.html to templates/index.html), and automatically have the javascript emit my custom 'set_nick' event with the username automatically upon connect.
I defined LOGIN_URL = '/accounts/login' in settings.py, included 'django.contrib.auth.urls' in my urls.py and wrapped the index view with #login_required.
It was only then that I noticed that no matter what URL you request, you always get the chat apps index view - no login page redirect, '/admin/' is ignored, etc.
EDIT Solved - See my answer below.
I noticed that the urls.py which I blindly copied over from the example looked like this:
url(r'', include('socketio_app.urls')),
url(r'^admin/', admin.site.urls),
and the r'' was the culprit (matches everthing). Changing this to:
url(r'^/', include('socketio_app.urls)),
url(r'^admin/', admin.site.urls),
However, I'm using Django==1.11. I believe Django 2 tends to suggest using path (or some similarly named function) rather than using url. I don't believe, however, that the semantics of url are different in Django 2, so this is probably an issue for Django 2 users as well.
I'm trying to look for the login page. I've created a folder registeration/login.html under templates, but it doesn't read my updated file at all.
Secondly, there's a button that says login but takes me to accounts/login page, how do I just forward the url to accounts/login then? and where do i customize the login page? I've tried creating an accounts/loginunder templates, that doesn't work.
urls.py
url('^', include('django.contrib.auth.urls')),
It is always a good idea to check out the source code of the framework when you're looking for the answer. In django.contrib.auth.views.LoginView default template is:
template_name = 'registration/login.html'
{% url 'login' %}
Use to retrieve pages from template (html)
I realize this is very late, but will hopefully be helpful for others facing similar issue.
This is the Mezzanine source code for login page which Mezzanine renders by default.
So, you can do something like
python manage.py collecttemplates -t accounts/account_login.html
to copy the above file to your app template directory, and modify the copied file as per your needs.
Bonus: You can find other account related templates (like sign up, password reset, etc.) here.
I've got a base url. http://baseurl.com/
I'm trying to run projects on the back of it. For example
http://baseurl.com/mongoose/
The projects run but the URL don't work properly because they all
reference the base url. So for 'About Me' page it points to
http://baseurl.com/about instead of http://baseurl.com/mongoose/about
Is this something i need to change in django or apache? Is what I'm
trying to do even possible?
Coming from an IIS .net background I know that in IIS you can "Create and application" within a site which essentially does what I'm trying to achieve now with Apache and Django.
Thanks
You shouldn't need to do anything. Apache is supposed to be setting a request header called SCRIPT_NAME, which is your base URL, and all URL reversing takes that into account.
How are you creating these URLs in your templates?
Update
So your problem is with getting the URLs of Flatpages. The issue is that the normal way of calculating URLs dynamically, so that they do take SCRIPT_NAME into account - using the reverse() function or the {% url %} tag - doesn't work with Flatpages, because they are not dispatched via urls.py but via a custom middleware which fires on a 404.
So instead of using that middleware, I would use the urls.py mechanism to dispatch to flatpages. Remove the flatpagemiddleware from your settings.py, and in urls.py at the end of your patterns add this:
url(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage', name='flatpage'),
Now, in your templates, you can do:
<a href="{% url flatpage page.url %}">
and it should work correctly.
Check any urls.py in the project(s) to see if they expect to be top-level. But if the application outputs links like /something then it's going to mean the root directory. The application should be reversing a view/parameter into a URL, which would allow you to move it around. If you wrote the apps, check out reverse in django.core.urlresolvers