Pycharm unresolved references with Django - django

This is my 5th day learning Django. I've got a Django project working in a virtual environment using Pycharm. The problem I have is, Pycharm can't reference certain imports when they are actually working in Django without any issues. I've selected the right interpretor for the project but Pycharm cannot connect the imports with the .py files in the Django project. The selected interpretor does have Django for the virtual environment but it still doesn't work. Below are the screenshots:
import works in Django but Pycharm cannot resolve reference with the correct interpretor

You need to mark the project's root source. You can do this by right-clicking:
Or by going to PyCharm settings > Project: Project Structure > and make sure to mark the project root directory (the folder that holds your project's apps) as a "Sources" directory.
Sidenote: I noticed you're not using directory-level urls.py. I would suggest doing that instead of trying to import URLs from other apps into the project-level urls.py.
What this means is, each app - articles, accounts, etc. - would have a file named "urls.py".
In your project-level urls.py, you do this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
path('accounts/', include('accounts.urls')),
]
In your app-level urls.py, you do this:
from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name="hey"),
]

Related

Django/Wagtail Media Files not showing up in the admin portal on a fresh install

I am working on a site in dev that contains a media folder. When I do a fresh setup of the site (empty db) and do all the migrations and steps to get the site up and running I noticed in the admin portal none of the images and assets in the media folder dont show up even though they exist. I have to re-import an image and then it shows up in the admin portal as expected. I have looked all over and cannot find an answer. To put it simply why isnt the admin portal importing existing files in the media folder on a fresh setup?
django==3.2
wagtail==3.0
base.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'
urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.contrib.sitemaps.views import sitemap
from search import views as search_views
urlpatterns = [
...
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If anyone has any idea?
That is totally normal. Just as you need to import your page data into the database, you also need to import the information about your documents into the database (in addition to having the files). You can write a script to help with these imports. I don't have one for images but here is one I wrote for importing documents from a nested directory in the file system into a nested set of collections: https://gist.github.com/cnk/54031ca6775fa0d29997449a1e2010ec

How can I host a Django web app on Cpanel out the public_html folder?

I am trying to host a Django web application on Cpanel.
However, my hosting service is having a main folder which is called public_html. In this folder, there is the index page. My project folder named myapp is out of the public_html folder. Whenever I run the application, it is showing the content of the index.html which is in the public_html folder instead of running the home page of my application that should be executed from this main urls.py file.
Bellow is the main urls.py content.
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('depenses/', include('depenses.urls', namespace='depenses')),
path('cart/', include('cart.urls', namespace='cart')),
path('orders/', include('orders.urls', namespace='orders')),
path('coupons/', include('coupons.urls', namespace='coupons')),
path('', include('shop.urls', namespace='shop')),
# path('', TemplateView.as_view(template_name='templates/index.html'), name='home'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Again my project is out of the public_html folder.
Please assist me to host my application.
This is normal behavior considering the fact that the web service will attempt to serve HTML/PHP files only.
You will need to deploy your Django app through a python interpreter like Apache Passenger or something similar that would handle and serve it.
This can be easily achieved without many configurations and hassles with the Python App feature of CloudLinux.
Therefore, I would suggest you simply find a hosting provider that provides CloudLinux's Python App feature. The deployment process via that feature is with just a few simple clicks.

Improperly Configured urls.py during Django deployment (Django 2.1)

This is my first time deploying Django. My app runs fine locally, but when I deploy, I get this error:
ImproperlyConfigured at /admin/
The included URLconf module 'search.urls' from '/home/imeaytbc/myproject/search/urls.py' does not
appear to have any patterns in it. If you see valid patterns in the
file then the issue is probably caused by a circular import.
My urls.py file is exactly the same as the one run on my computer:
from django.urls import path
from . import views
app_name = 'search'
urlpatterns = [
path('', views.query_search, name='query_search'),
path('article/<int:ArticleID>/', views.article_detail, name='article_detail')
]
Is there anything I need to change in regards to deployment? All the changes I made to my files regarding deployment are about static and media file directories. What else do I need to change for deployment? As far as I am aware, I have uploaded all files to the hosting server and the app shouldn't be missing any file.
EDIT: added main urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('search/', include('search.urls')),]

Django URLs management

I'm a full beginner on Django. Therefore, I'm sorry if my question is not making so much sense.
I'm studying Django tutorial - step 1. I installed properly Django and created my first Django project named 'vanilla'. The urls.py script of the vanilla project is
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
When I start Django with this 'vanilla' project and start the development server at http://127.0.0.1:8000/, I correctly see the start page (with a rocket).
In a second project that I named 'djtutorial', I created as requested in Django tutorial - step 1 a 'polls' app. As requested, I modified urls.py file in djtutorial\polls which now has following content:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
If I start the server the url http://127.0.0.1:8000/polls/ is working properly. However, if I launch the root url of the site (i.e. http://127.0.0.1:8000/), I now have a 404 error.
Why is the 'root url' of the site now hidden?
There wasn't any root URL at all. Main page informing you that your project is created properly shows up only if you don't have any view of your own added to urlpatterns. When you create any page, this welcome page stops showing up and that is expected.
If you want to show your own page on root of your website, use this in your urlpatterns:
path('', >>VIEW OR INCLUDE HERE<<),

Django views in project directory

I have a project with two apps with their respective views, but I want to create a views.py in django project directory for some generic pages such as about, login... It is correct to place the views.py and templates in the root project folder for this purpose?
I have been reading about this. The practice more recommended for this is create an app that creates cohesion between other apps, call it web_site or site whatever is better for you.
python manage.py startapp my_site
Everything in this site app will be done the regular way.
In the projects url you will want to import the url in this way so the web pages appear in the / url pattern.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_site.urls'))
]
I do this for project level pages like you describe. These are usually simple pages that tie the project's apps together, and not contain any business logic.
You can do that by making views.py file into the project directory.
from django.contrib import admin
from django.urls import path, include
from attendance import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('insert.urls')),
path('login/', views.logins, name='login'),# relevant line
]
And this is the code of login view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def logins(request):
return HttpResponse("THIS IS LOGIN PAGE !!!")