I need to import views from horoscopes and dates, but when importing them into urls.py there is a conflict because of which only one views is perceived.
So my question is: how do I import multiple views?
I've tried several approaches, e.g.:
from django.contrib import admin
from django.urls import path
import horoscopes
import dates
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/leon', horoscopes.views.monday),
path('dates/monday', dates.views.monday),
]
as well as this one:
from django.contrib import admin
from django.urls import path
from horoscopes import views as horoscopes_views
from dates import views as dates_views
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/leon', views.leon),
path('dates/monday', views.monday),
]
But both options still ignore one of the views.
As voodoo-burger was suggesting, the browser can only display one page at the time. So either rename your URLs to more specific names in order to differentiate between the views, or you are looking for URL Namespacing.
From the Django Documentation:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
...
]
Then call your URL like this:
reverse('polls:index', current_app=self.request.resolver_match.namespace)
You can try following code
from django.contrib import admin
from django.urls import path
from horoscopes import views as horoscopes_views
from dates import views as dates_views
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/leon', horoscopes_views.leon),
path('dates/monday', horoscopes_views.monday),
]
I found the most optimal solution to my problem and it consisted in creating a separate file in each application urls.py , separately adding all URL addresses there in this form:
in each of my applications, the necessary horoscopes and I added a separate file called urls.py and I entered the following into it:
from django.urls, import the path
from . import views # the dot indicates that we are referring to the directive in which the file is located
urlpatterns = [
path('scorpion/', views.scorpion),
path('leon/', views.leon),
]
and according to the same scenario, I added to another application for the url
from django.utils, import the path
from . import views
urlpatterns = [
path('monday/', views.monday),
]
The main one includes the application urls.py it looks like this:
from django.contrib, import admin
from the django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/', include("horoscopes.urls")),
path('dates/', include("dates.urls")),
]
Related
I'm not able to access the url named "forum" using the reverse function. It gives me this error:
django.urls.exceptions.NoReverseMatch: Reverse for 'forum' not found. 'forum' is not a valid view function or pattern name.
I'm able to access all my other namespaces URLs within the other apps. But I just can't access anything by name from my root URLs.py file.
Here is my root URLs.py file
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls), name='forum'),
path('admin/', admin.site.urls),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Remove:
from machina import urls as machina_urls
Change the url to this:
path('forum/', include(machina.urls),),
in the urls.py file of machina add above the urlpatterns:
app_name = 'machina'
If you are going to use reverse remember:
reverse('app_name:url_name')
For example we have the following url in our machina.urls:
path('bitcoin/', views.bitcoin, name='bitcoin'),
Your reverse would look like this:
reverse('machina:bitcoin')
docs: https://docs.djangoproject.com/en/3.1/ref/urls/#include << Class based overview
Examples: https://docs.djangoproject.com/en/3.1/topics/http/urls/#namespaces-and-include << Copy and write overview
Other option is to have a separate file with url pattern lists for example:
url_list.py
from django.urls import path
from yesyes import views
varvar = [
path('', views.testbased, name='index'),
]
urls.py:
from django.urls import path, include
from yesyes.url_list import varvar as urllist
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',include(urllist), name='test'),
]
I was trying to call one html file from another via Django.
I actually have no errors inside my project(It works). But when I am trying to call the second html file the page refreshes only. Here is my code, thanks:D. My projects name is information and the name of my app is basic.
basic.views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'basic/index.html')
def customer(request):
return render(request,'basic/customer.html')
basic.urls.py:
from django.urls import path
from basic import views
app_name='basic'
urlpatterns=[
path('',views.index,name='index'),
path('',views.customer,name='customer'),
]
information.urls.py:
from django.contrib import admin
from django.urls import path,include
from basic import views
urlpatterns = [
path('',views.index,name="index"),
path('admin/', admin.site.urls),
path('',include('basic.urls'))
]
The problem is that you have two paths that match exactly the same path. So that means each time you make a request with an empty path, the first one is used.
You should make use of non-overlapping paths, for example:
# basic/urls.py
from django.urls import path
from basic import views
app_name='basic'
urlpatterns=[
path('', views.index, name='index'),
path('customer/', views.customer, name='customer'),
]
and in your information/urls.py:
# information/urls.py
from django.contrib import admin
from django.urls import path,include
from basic import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('basic/', include('basic.urls'))
]
*from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('products/',include('products.urls')),
path('accounts/',include('accounts.urls')),
]*
The above code of pyshop/url.py
I could not login to admin page.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('products/new',views.new),
path('products/',views.products),
path('accounts/register',views.register),
]
The code is of product/urls.py.
Old question, but I run into the same problem, so just in case I can help someone else.
I would put in the pyshop/url.py, this code (see the admin path is the only one with an actual url):
from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('products.urls')),
path('',include('accounts.urls')),
]
Then in your product/urls.py (See all of the routes end with a slash):
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="product"),
path('products/new/',views.new, name="new_product"),
path('products/',views.products, name="products"),
path('accounts/register/',views.register, name="register"),
]
I started working with Django yesterday and I have a little error. I searched on the internet but I cant find the answer for my problem. I made the main app and then I make a little app and tried to link it to the main. Here is my code
urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('^$', include('personal.urls'));
]
in my personal.urls I have
from django.urls import path, include
from . import views
ulrpatterns = [
path('^$', views.index, name="index");
]
and in views.index I have
def index(request):
return render(request, 'personal/home.html')
Any help would be awesome, thank you!!!
You are using the wrong tool here: since django-2.0, there are basically two ways to specify a URL:
with a regex through re_path [Django-doc], url [Django-doc] is an "alias" of this, but will probably eventually get deprecated; and
by using a specific "path pattern" syntax with path [Django-doc]
Here you somehow mixed the two, and use path(..) function for a "regex-specified" URL.
Using path for path pattern-based URLs
We can also fix it by using path:
# urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import path, include
from . import views
ulrpatterns = [
path('', views.index, name="index");
]
Using re_path for regular expression-based URLs
We can also fix it by using re_path instead:
# urls.py
from django.contrib import admin
from django.urls import re_path, include
ulrpatterns = [
re_path('admin/', admin.site.urls),
re_path('^$', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import re_path, include
from . import views
ulrpatterns = [
re_path('^$', views.index, name="index");
]
I have read many answers in this forum, but they does not solve my problem. I will be very grateful for help.
My file views.py returns this error:
from . import views
ImportError: cannot import name 'views' from '__main__' (C:/Users/tymot/Desktop/weather app/env/Environemnt/the_weather/weather/views.py)
views.py (Environemnt\the_weather\weather)
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html
urls.py (Environemnt\the_weather\weather)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
urls.py (Environemnt\the_weather\the_weather)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
templates(the_weather\weather\templates\weather)
only file index.html
Directory
-the_weather
--the_weather
---__init__
---setting
---urls
---wsgi
--weather
---migrations
----__init__
---templates
----weather
-----index
---__init__
---admin
---apps
---models
---tests
---urls
---views
--db
--manage.py
I try use to resolved my problem from __future__ import absolute_import, or homepage import views. I else try copy views.py to directory templates (and modify its code) but unfortunately it not work
You need to separate your views and urls create a new module (file) urls.py in your app, in your case it is weather folder, and add these code there, and remove it from views.py, you can read here about it to understand it better.
Path : the_weather/weather/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
Path : the_weather/weather/views.py
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html template
You would need to put your views.py file in your weather folder. Make sure it is in the right weather folder, I am guessing you have two weather folders. Also make sure these codes are right;
#Path : the_weather/weather/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
#Path : the_weather/weather/views.py
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html template
Why don't you try like this
from .views import index
There may be separate folder of urls and views.py
what you have to do is you have to write
from appname import views
suppose your app name is myapp
you have have to write
from myapp import views
use should create new url.py in weather and write code in views.py
url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
write code in urls.py of your project
from django.urls import path, include
from . import views
urlpatterns = [
path('wheather/',include('wheather'))
]
I had Similar problem, it didn't solved, till I added an empty views.py in root in example, in your case add to Environemnt\the_weather\the_weather
I had the same problem.
In formsdemo\formsdemo\__init__.py, I changed the import code to from forms app import views.
from django.contrib import admin
from django.urls import path
import main.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', main.views.home_page, name="home_page"),
]
Well, i was also facing the same issue. I was continuously getting server errors but then I figured out it was a simple error.
go to the urls.py file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = index), #the path for our index view
]
I just had to remove name = index and the server worked with 0 errors.
enter image description here