how to work with two apps in django - python

Hello im learning django and i would like to know if its a way to make my two apps work i have my folders like this
mysite/
--/app1 (blog app)
--/app2 (a list of users that signs up with a form)
--/mysite
--db
--manage.py
my app2 has the index.html and the structure.html which i use to inheritate my others html files, so
im trying to use the post_list.html file that i made in my app1, first of all this is how my urls looks something like this
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'info', 'app2.views.grid'),
url(r'', 'app2.views.home'),
url(r'^blog/', 'app1.views.post_list')
)
my app1 views looks like this
from django.shortcuts import render
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.objects.filter(published_date_isnull=False).order_by('published_date')
return render(request, 'app1/post_list.html',{'posts':posts})
then my post_list.html looks like this
{% extends "/app2/templates/app2/structure.html" %}
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaks }}</p>
</div>
{% endfor %}
{% endblock %}
when i enter to my browser and type 127.0.0.1:8000/blog/ it keeps apearing my app2/index.html, what am i missing? do i have to do anything else than adding my views to my url like i did?
aditional info: i added my app2 and my app1 to my settings thoe

I think your urls.py needs to be updated:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^info', 'app2.views.grid'),
url(r'^$', 'app2.views.home'),
url(r'^blog/', 'app1.views.post_list')
)
url(r'', 'app2.views.home') will act like a wild card since python uses regular expressions to match. Take a look at the URL dispatcher documentation for a better understanding of what's going on.

Related

Django shows page not found

I'm learning Chapter 18 18.4.2 in Python Crash Course,when i open http://localhost:8000/topics ,I'm using Django 3.0 and python 3.8
shows
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, topics, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
and this is my code
learning_log\urls.py
from django.contrib import admin
from django.urls import path
from learning_logs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name = 'index')
]
learning_logs\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name = 'index'),
url(r'^topics/$',views.topics,name='topics')
]
views.py
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
return render(request,'learning_logs/index.html')
def topics(request):
topics = Topic.objects.order_by('date_added')
context = {'topics':topics}
return render(request,'learning_logs/topics.html',context)
base.html
<p>
Learning Log-
Topics
</p>
{% block content %}{% endblock content %}
topics.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>{{ topic }}</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
and runserver shows:
Not Found: /topics
[06/Jan/2020 17:53:15] "GET /topics HTTP/1.1" 404 2077
enter image description here
First of all, good question. Love the detail.
The error is the following
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order
This means that the url /topics cannot be found. You need to import that url from the app specific urls.py into the main urls.py. This is usually done by something along the lines of
# in main urls.py
from learning_logs.urls import urlpatterns as ll_urlpatterns
# other patterns ...
urlpatterns += ll_urlpatterns
Use below code replace 'app' with your appname.
from django.contrib import admin
from django.urls import path
from learning_logs import views
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include(('app.urls', 'app'), namespace='app')),
]

loading images by list view in django template

I am trying to render by django template with photos saved in database by using listview so they can act like thumbnails like that of amazon.com but images are not loading
{% for offer in offer_details %}
{% if offer == None %}
<img src="{% static "pics/s7.jpg" %}" class="im">
{% else %}
<img src="{{offer.photo.url}}">
{% endif %}
{% endfor %}
views.py
class Index(ListView):
context_object_name = 'offer_details'
model = models.Offer_discription
template_name = "index.html"
from django.contrib import admin
from django.urls import path,include
from interface import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls, name="adi"),
path("",views.Index.as_view(), name="index"),
path("interface/",include("interface.urls")),
path("logout/",views.user_logout, name="logout"),
path("special", views.special,name="special"),
path("<int:pk>/", views.OfferDetailView.as_view(), name=" OfferDetailView")
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I was not adding that last line of code and last two imports

why isn't this django url redirecting?

After getting post data from the following form, the page should redirect to 'associate:learn' as shown in the action. However, it just stays on the radio button page. I suspect I'm making a beginner's error but after rereading the tutorial, I'm not sure what's going on.
index.html
Choose a dataset
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'associate:learn' %}" method="post">
{% csrf_token %}
{% for dataset in datasets %}
<input type="radio" name="dataset" id="dataset{{ forloop.counter }}" value="{{ dataset.id }}" />
<label for="dataset{{ forloop.counter }}">{{ dataset }}</label><br />
{% endfor %}
<input type="submit" value="learn" />
</form>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^images/', include('images_app.urls', namespace="images_app")),
url(r'^associate/', include('associate.urls', namespace="associate")),
url(r'^admin/', include(admin.site.urls)),
)
views.py
def index(request):
images = Image.objects.all()
datasets = []
for i in images:
if i.rank() >= 3:
datasets.append(i)
return render(request, 'associate/index.html', {'datasets':datasets})
The original HTML should redirect to this page.
learn.html
THIS IS THE LEARN PAGE
Can you go to associate:learn directly?
In your first urls.py
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
The url will always match "associate.views.index" since it appears before "associate.views.learn" and they both have the same url.
You should change it to something like:
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^learn_or_something$', "associate.views.learn", name='learn'),
)
Hope this helps.
Your associate "index" and "learn" views both have the same URL. You need to have some way of distinguishing between them, otherwise the URL will always be served by the first one, which is index.

Django reverse error: NoReverseMatch

I've looked at a lot of different posts, but they're all either working with a different version of django or don't seem to work. Here is what I'm trying to do:
urls.py (for the entire project):
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
)
urls.py (specific to the app):
urlpatterns = patterns ('' ,
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>[\w\-]+)/$', views.posts, name="postdetail"),
)
views.py:
def index(request):
posts = Post.objects.filter(published=True)
return render(request,'blog/index.html',{'posts':posts})
def posts(request, slug):
post = get_object_or_404(Post,slug=slug)
return render(request, 'blog/post.html',{'post':post})
And finally the template:
{% block title %} Blog Archive {% endblock %}
{% block content %}
<h1> My Blog Archive </h1>
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url "postdetail" slug=post.slug %}">
{{post.title}}
</a>
</h2>
<p>{{post.description}}</p>
<p>
Posted on
<time datetime="{{post.created|date:"c"}}">
{{post.created|date}}
</time>
</p>
</div>
{% endfor %}
{% endblock %}
For some reason this gives me a "No reverse Match": Reverse for 'postdetail' with arguments '()' and keyword arguments '{u'slug': u'third'}' not found. 0 pattern(s) tried: []
I've already tried getting rid of the double quotes around postdetail in the template, and I've also tried referring to it by the view name instead of the pattern name. Still no luck. The documentation isn't too helpful either.
Help is really appreciated! Thanks
You've used a namespace when including the URLs, so you probably need to use "blog:postdetail" to reverse it.

How to embed the results of my django app inside a template using something like {% include "app" %}?

So far I have been able to Create a project and setup a Homepage. So far I have had success with styling the page and getting my nav areas setup. I have also created an app that pulls a list of category names from my database and displays it in a right-justified list. When I point my browser to the app url, it works perfectly, but when I try to include the view in my project it displays the base panel with an error, and the dictionary I passed to the view does not appear to be available.
This is what I get when I load the home url localhost:8000/ in my browser:
This is what I get when I load the app url localhost:8000/categories/ in my browser:
Why am I not able to push the results of my app into my template? Both appear to work but not together?
base_right_panel.html
{% block content %}
<div style="float: right;">
<div id="base_categories" style="margin: 10px; padding-bottom: 10px;">
{% block base_categories %}
{% include "base_categories.html" %}
{% endblock %}
</div>
</div>
{% endblock %}
base_categories.html
{% block content %}
<div class="section" style="float: right;">
<h4 class="gradient">Category List</h4>
<ul>
{% if categories %}
{% for category in categories %}
<li>{{ category.title }}</li>
{% endfor %}
{% else %}
<p>no data! {{ categories|length }}</p>
{% endif %}
</ul>
</div>
{% endblock %}
CategoryList/views.py
from django.views.generic import TemplateView
from CategoryList.models import CategorylistCategorylist #<-- Changed to match inspectdb result
class IndexView(TemplateView):
template_name="base_categories.html" #<-- Changed name from index.html for clarity
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context["categories"] = CategorylistCategorylist.objects.all()
return context
CategoryList/models.py
from django.db import models
class CategorylistCategorylist(models.Model): #<-- Changed to match inspectdb
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255L, unique=True)
base_url = models.CharField(max_length=255L, unique=True)
thumb = models.ImageField(upload_to="dummy", blank=True) #<-- Ignored inspectdb's suggestion for CharField
def __unicode__(self):
return self.name
# Re-added Meta to match inspectdb
class Meta:
db_table = 'categorylist_categorylist'
CategoryList/urls.py
from django.conf.urls.defaults import patterns, url, include
from django.contrib import admin
from django.conf import settings
from CategoryList import views
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='base_categories'),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
MySite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from home import views as home_view
from CategoryList import views as index_view
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', home_view.HomeView.as_view(), name="home"),
url(r'^categories/$', index_view.IndexView.as_view(), name='base_categories'),#include('CategoryList.urls')),
url(r'^admin/', include(admin.site.urls)),
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
I have another open question which has the relevant code examples but the question differs from what I am asking here.
Is there a simple way to display mysql data in Django template without creating an app?
You need to add the category queryset to your context in your HomeView. Remember, the view uses the templates to build the response - including a template that you also use in a different view (IndexView) does not cause any interaction with IndexView.
HomeView produces its response by rendering a template. If that template uses {% include %} tags to pull in pieces of other templates, those pieces will be rendered using the context established by HomeView. Nothing you do in IndexView has any effect on HomeView, and vice versa.
Continuing to reason by analogy to string interpolation, pretend your templates are global string variables instead of files on disk. Your situation is something like this:
base_categories = "My categories are: %(categories)s."
base_right_panel = "This is the right panel. Here are other fields before categories."
Using the {% include %} tag is something like string concatenation:
base_right_panel = base_right_panel + base_categories
Then your two views are analogous to this:
def home_view(request):
context = {}
return base_right_panel % context
def index_view(request)
context = {'categories': ['a', 'b', 'c']}
return base_categories % context
Unless you add the categories queryset to the context in HomeView, it will not be available to the template engine when rendering the response.
Your HomeView class should include the get_context_data method that you currently have in your IndexView. I am not sure you actually need IndexView at all, unless you want to have something that serves that page with just the categories list.

Categories

Resources