<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Max</title>
    <description>The latest articles on DEV Community by Max (@max_dobeck).</description>
    <link>https://dev.to/max_dobeck</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F591658%2F9de0590d-5ed1-4744-8f16-735b127ddda0.png</url>
      <title>DEV Community: Max</title>
      <link>https://dev.to/max_dobeck</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/max_dobeck"/>
    <language>en</language>
    <item>
      <title>Simple Django w/ Svelte</title>
      <dc:creator>Max</dc:creator>
      <pubDate>Wed, 24 Mar 2021 00:25:41 +0000</pubDate>
      <link>https://dev.to/max_dobeck/simple-django-w-svelte-4li4</link>
      <guid>https://dev.to/max_dobeck/simple-django-w-svelte-4li4</guid>
      <description>&lt;h1&gt;
  
  
  Forewarning
&lt;/h1&gt;

&lt;p&gt;If you are serious about using Django and Svelte you probably just want &lt;a href="https://pypi.org/project/djangorestframework/"&gt;djangorestframework pkg&lt;/a&gt; and &lt;a href="https://pypi.org/project/django-svelte/"&gt;django-svelte pkg&lt;/a&gt;. This will get you two major things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pass prop data to svelte components&lt;/li&gt;
&lt;li&gt;POST/PUT/UPDATE/DELETE data via API calls from svelte components. Aka modify and submit data, turning this into a useful two way street as you won't be able to use Django's nice builtin forms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Final Example: &lt;a href="https://gitlab.com/mdpb/svelte-django-example"&gt;https://gitlab.com/mdpb/svelte-django-example&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Goal
&lt;/h1&gt;

&lt;p&gt;I figured &lt;a href="https://rollupjs.org/guide/en/"&gt;rollup&lt;/a&gt; turned svelte into a basic .html and .js file, so why not just host those from specific routes in Django? &lt;/p&gt;

&lt;p&gt;This way you could rely on the templating in Django, stay in Python land as long as possible, then push a Svelte component or mini-Singe Page Application to the user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why?
&lt;/h3&gt;

&lt;p&gt;Some of you may think this setup sounds like old news, or hail from the olden times of Apache servers and the /www directory.  But for me the easiest and most familiar path is working 100% on a Single Page App with react/svelte/vue and then pushing it to a CDN via some service like &lt;a href="https://vercel.com/guides/deploying-svelte-with-vercel"&gt;Vercel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After the frontend code, written in 100% modern javascript, then you make &lt;code&gt;fetch&lt;/code&gt; requests to populate the site with data.  So you end up making CORS requests.  That works fine, little learning curve with CORS if you own the resource you're fetching from.&lt;/p&gt;

&lt;p&gt;But it lacks a certain simplicity.  I wanted to live on one single domain and manage everything from a single language + framework as much as possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting into it
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Build your svelte component(s) locally.&lt;/li&gt;
&lt;li&gt;Compile your svelte page(s) and send them to the &lt;code&gt;static&lt;/code&gt; directory for your Django app.&lt;/li&gt;
&lt;li&gt;Return a Template from your Route's View like normal, except the .html file you return is gonna have a few extra &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tags:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;script 
        type="module" 
        src="{% static 'notes/bundle.js' %}"&amp;gt;
    &amp;lt;/script&amp;gt;
    &amp;lt;link 
        rel='stylesheet' 
        href="{% static 'notes/bundle.css' %}"&amp;gt;
    &amp;lt;link 
        rel='stylesheet' 
        href="{% static 'css/global.css' %}"&amp;gt;
&amp;lt;/head&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This won't leave you with much, you'll be able to send a svelte file off to the user but &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you won't be able to push Django data into it&lt;/li&gt;
&lt;li&gt;you won't be able to submit data as simply as standard Django form&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But hey! You're pushing svelte out client side!&lt;/p&gt;

&lt;h3&gt;
  
  
  Details
&lt;/h3&gt;

&lt;p&gt;Start with your URL route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#your_app/urls.py
&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'new/svelte/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;svelte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'new'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll need a View for that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#your_app/views.py
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;svelte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'notes/new-svelte.html'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And of course a Template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- your_app/template/your_app/new-svelte.html --&amp;gt;
{% load static %}
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;script type="module" src="{% static 'notes/bundle.js' %}"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;link rel='stylesheet' href="{% static 'notes/bundle.css' %}"&amp;gt;
    &amp;lt;link rel='stylesheet' href="{% static 'css/global.css' %}"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;noscript&amp;gt;ah shucks. you should see a svelte component, 
not this message :/ enable javascript if you wanna use svelte&amp;lt;/noscript&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you copy pasted the above code you should be seeing your svelte app when you go to &lt;code&gt;localhost/your_app/new/svelte&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serving Static Files in Django
&lt;/h3&gt;

&lt;p&gt;You may need to add &lt;code&gt;'django.contrib.staticfiles'&lt;/code&gt; to your settings.py INSTALLED_APPS[] but this is worth reading: &lt;a href="https://docs.djangoproject.com/en/3.1/howto/static-files/"&gt;https://docs.djangoproject.com/en/3.1/howto/static-files/&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Sources:
&lt;/h1&gt;

&lt;p&gt;This article was instrumental: &lt;a href="https://dev.to/ashraf_zolkopli/django-svelte-best-dev-experience-dx-1848"&gt;https://dev.to/ashraf_zolkopli/django-svelte-best-dev-experience-dx-1848&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the raw source is here: &lt;a href="https://gitlab.com/mdpb/svelte-django-example"&gt;https://gitlab.com/mdpb/svelte-django-example&lt;/a&gt;&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>django</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
